Useful Git Tips and Commands

Git is an extremely powerful tool with a big and rich set of tools to help you maintain your projects. In this article, we are going to look at a couple of useful git tips and commands that I find useful in my daily life. Hopefully, you will learn some new useful features to add to your toolbox while working with git.

Git diff

Quite often you will end up working on a separate branch and you will want to compare a branch to another one. This can be done with git diff branch1 branch.

If you want to show changes in the index (staged changes) against the current HEAD git diff --cached is extremely handy. This is different compared to the normal git diff which only shows changes not yet added to the index.

Revert staging to index

If you staged some file to the index but changed your mind, then there are a couple of different commands you can use depending on what outcome you want to achieve.

git rm path/to/file --cached deletes from the index but keeps the changes in your working directory. Which is different and much safer than git rm file -f which deletes the file completely.

Git reset

If you want to revert changes that you have done, you can use the git reset command. There are various different flavors of a git reset, so make sure you use the appropriate depending on what you want to achieve.

If you want to scratch everything, including the index and your working directory then you can use git reset —hard. If you instead want to reset the index you can instead resort to git reset --mixed which is also the default when you simply type git reset. Doing a mixed reset will leave the changes in your working directory. If you are just interested in moving the branch and don’t change any of the areas, then you can use a git reset —-soft which only moves the HEAD.

When running a git reset command you can also specifically target files by as the command accepts a list of files.

It is also possible to reset to a specific commit by providing the commit id git reset —hard COMMIT_ID.

Git stash

You might be aware of the git stash command which resets the changes, in a non-destructive way as you can easily pop it back by running a git stash pop. However, if you have any untracked files then they won’t be stashed by default. To also stash untracked files you can use git stash —include-untracked. Another useful command is git stash list which lists the contents of the stash.

History

Git comes with some powerful and useful tools to view what has happened in the project and specific files. One of my favorite commands is showing a decorated graph of the commit history which can be done with the following command git log --graph --decorate --oneline. It’s quite a long command so I recommend you to create an alias for it in your terminal (which by the way is perhaps one of the best git tips that you can get, since many commands may be difficult to remember). The git log command will show branches, where the HEAD is and also all the commit ids. You can then use git show COMMIT_ID/HEAD/BRANCH to show more detailed info.

Sometimes it’s useful to be able to know who changed what in a file, git blame path/to/file lets you view exactly who changed what.

Git diff, as mentioned earlier, is also a tool that views history. For example, to compare the current HEAD to two commits earlier you can use the command git diff HEAD HEAD~2.

To see more detailed information about what changes each commit introduced, use the git log --patch command. To only see a commit with the keyword apple in them, use git log --grep apples --oneline.

To see the commits between two points in the history you can use the following command git log HEAD~5..HEAD^ --oneline or, for example, git log branch_name..master --oneline.

Fixing mistakes

Warning: When it comes to rewriting history, please make sure that you know what you are doing!

Sometimes you may want to edit history, such as when you have made a mistake. However, I do not recommend that you change history that has already been pushed to the remote repository (even though git lets you do that). However, personally, I think it is perfectly fine to edit history that you have in your local repository. To remove your previous commit you can use git commit --amend which will create a new commit which sort of replaces your previous commit.

Another popular command, which could be one my favorite git tips, is the interactive rebase. Which can be used to edit commit messages or my favorite feature; to squash multiple commits into a single commit. To perform an interactive rebase on all your commits that you performed after the state of origin on the master branch the following command can be used git rebase -i origin/master. This will present you with a list of the commits and a good description of what you can do. For example, the following will squash multiple commits into one.

1 pick 80f2a48 Add feature X
2 squash 2c74ea2 Add junit tests for feature X
3 squash 4k81nm5 Bugfix for feature X

The outcome will be one commit with the message “Add feature X”.

If you want to revert a broken commit you can use git revert COMMIT_ID which will create a new commit where the project is in the state before that commit.

If you managed to screw up when fixing mistakes, let’s say that you accidentally delete something that you didn’t intend to. You can still recover from that as git keeps logs of everything that has changed, including removed commits. To do that, use git reflog

Cherry-picking

Let’s say that you and a colleague are working on separate branches, and your colleague made an awesome commit that you also want into your branch, but you don’t want all the other changes. This is where you could use git cherry-pick COMMIT_ID.

Final Words

These were my favorite git tips. Hopefully, you learned something new. These are some commands that I find useful from time to time and they have definitely helped me in my daily work. Do you have any other good git tips of some tools that you use frequently or some command that have saved you loads of time? Please share them in the comment section!

Leave a Reply