Learn Git

Various Features In Git – Git Series Part 5

git series Various Features In Git

Now that we have seen how you can use a GUI or IDE to interact with a repository, let’s look at the many features that git offers to make life easier. I will demonstrate how to amend the previous commit message, how to see what files were changed in a commit, rebasing, and tools to help with troubleshooting. Follow complete Git series here.

Revision

Let’s look at the man page again to see what we have to work with. We have already discussed committing, pushing, pulling and merging changes. We touched upon rebasing and branches, but here, we will expand on those. Also, we will go over some more features not yet discussed. We can see the commit history. Now we can have a look at the man page.

Did you know there is a man page for each git command? Here is an example:$ man git commit

man git revision command

We have detailed information about the commit command. We have already used two options (‘-a’ and ‘-m’). The ‘-a’ is a shortcut for adding modified files to the index. New files will still need to be added using ‘git add’. The ‘-m’ parameter allows you to specify the commit message in the commit command. What I want to draw attention to is the “–amend” option. This simply allows you to add changes to a previous commit.

man git commit

Here’s an example: you have committed a set of changes and you realize you left out a particular you had to include. You haven’t pushed yet. You could do a git reset and redo the commit, or you can simply amend it. It will take your previous commit, add any staged changes, and apply with the same message or a different one.

git commit amend

​One word of caution. You should do this before you push to remote. Otherwise, people with the original commit may need to resolve merge conflicts, and they’re annoying enough to deal with anyway.​What about some of the other parameters? Let’s have a look at some.

Removing Files

You have seen how to remove files from the tracker. You may have also found that you can use git to remove the files altogether using git rm. This will have git know that a file was removed and that removal can be committed.

$ git rm expanding.html
$ git commit -a -m “Removed an unwanted file” Untracked files can safely be deleted normally using Linux ‘rm’.

Stashing Changes

Git makes it easy to put aside changes that you don’t want to commit, nor lose, but need to put them away somewhere. Rather than manually putting them somewhere outside the repo, you can simply use ‘git stash’. This will place your changes away in a hidden section in the repository.$ git stashThis saves you having to copy the changes elsewhere and then checking out the branch, and all the other tasks to restore your changes. To reapply the stashed changes, use ‘git stash apply’. To remove the most recent stash, run ‘git stash drop’.

You can also specify which stash to apply or drop using this format: “stash@{2}” for the third most recent stash. In the man page for git stash, it states that you are able to specify a stash made around a certain period:

“stash@{3.hours.ago}”
$ git stash apply stash@{1}
$ git stash drop stash@{2}

Here is another example:

git stash

Rebasing

In addition to the merge tool, git also provides the rebase method for importing commits from one branch to another. The full details are available, but here is an overview. Rebasing takes the two branches, finds their common ancestor, and replays each commit from there in chronological order. In an earlier guide, we saw how to rebase worked in conjunction with ‘git pull’ using the ‘–rebase’ parameter. I use this to avoid having to compile merge commit messages.

​This differs from a merge. As mentioned previously, the normal merge will perform a three-way merge and if there are any conflicts, you will need to resolve them yourself.

Here is an example of the rebase command:

$ git checkout master
$ git rebase testing

Here is an example of a rebase where you need to resolve a conflict.

git rebase 1
git rebase 2

In this case, simply follow the prompts and resolve the conflict, like you would when attempting a git merge. If ‘git rebase –continue’ says there are no files added, yet you have already run ‘git add’, then simply run ‘git rebase –skip’.

git rebase 3

This will achieve the same result as a merge would, just using a different method.

Finding Out What Changed And By Whom

Git has tools that allow you to find out what was changed between commits, and who made those changes. These git commands include ‘diff’, ‘blame’, and ‘whatchanged’.

Diff

The command ‘git diff’ is used to show differences in individual files between commits. Differences between the current version of a file and in a commit can be viewed. You can also compare versions of the same file between commits.

git diff

Blame

Git is not only able to tell you what changed in a file, but also who made those changes, and when. ‘Git blame’ displays the changes that were made in a file and shows who made the changes.

​$ git blame index.html

Whatchanged

git whatchanged command

This is similar to ‘git log’. Like ‘git log’, it shows the list of commits. In addition to that, though, it displays what files have been changed in each commit. You can also specify what changed since the last git update, or since a certain time specified.

$ git whatchanged –since=”4 days ago”

Other Useful Tools

This is by no means a complete list. Git has a lot of other features under its belt, too many to cover here. I will list two other commands for git.

Grep

Git has its own built-in grep tool which you can use to find occurrences of a word, phrase, or pattern. If you look in the man page (man git grep), you can see that many of the options that you can use are identical to those for the Linux grep.

git grep

For finding matching patterns:$ git grep GitCase insensitive matching:

$ git grep -i 

GitList only the lines without a match:$ git grep -v GitSearch through all files in the repository regardless of whether they’re being tracked by git or not.$ git grep –untracked Git

Archive

As you have seen in the previous guide about GUIs, Git can also create an archive of a tree in either tar, gzip (tar.gz or tgz), or zip format. Use the ‘-l’ parameter to list the possible format options.

$ git archive -l

The syntax is very simple. Use the ‘-o’ parameter to output the contents to a file. The ‘-v’ parameter will display the files being added to the archive on the terminal.$ git archive master -v -o ../website-master.tgz

git archive command

Conclusion

We have gone over several features of Git that will be very useful for managing files, commits, repositories, and backing up your repository and changes. In the next guide, we will look at submodules within a repository.

Our sponsor - Deploy your server within 3 minutes


Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More