« Back to News

Git Tips and Tricks – Part II (Commits related)

In the earlier post we discussed some of the file-related git commands. Here, in this follow-up post, we see some important commit-related commands.

  1. Instead of making a new commit, fix previous commit

This one is pretty straightforward.  Let’s say we made a commit and then realized that we missed something or made a typo. We could make a new commit (correcting a typo/adding missed changes) with a “descriptive” message. But there is a better way for doing this by using:

git commit –amend

Git-Tips-&-TricksIf changes aren’t pushed to the remote branch yet, we can simply do the following steps:

  • Make the changes/fix typo
  • git add some-fixed-file.js
  • Run git commit –amend (This would add the most recent changes to the latest commit.)
  •  Push the single commit to remote.

2. Combining multiple commits into one

With Git, it’s possible to squash multiple commits into one. This is a great way to group certain changes together before sharing them with others.

Situations like this occur while working on multiple small tasks. Once we are done with 2 or 3 tasks, and later want to raise a single review request out of these 3 commits, then git squashing could be the BEST available option.

Let’s say this is our current Git log.

We have a branch “feature_x” here. We have already pushed “0e03cda” (proof of concept for feature X). After that we have been working on actual changes required for feature X along with addition of new elements to the feature and some changes in CSS. This means we want to squash these last three commits in one to make Git history look pretty simple. The command below would help to accomplish this:

git rebase -i HEAD~3

This will open up the editor with the following:

Now we can tell Git what to do with each commit. Let’s keep the commit 0a769cb, the one where we added our feature. We’ll squash the following two commits into this commit – leaving us with one clean commit with features X, including changes for the addition of new elements and CSS update changes. Change the opened file to this:

When done, save and quit your editor. Git will now squash the commits into one. All done!


Note: Do not squash commits that you’ve already shared with others. You’re changing history and it will cause trouble for others.

  1. Remove some files from commit

Suppose we add multiple files to a commit and later find that a file had mistakenly been added to the commit and we need to remove it. How can we remove that file from the commit?

The following steps would solve this problem:

Step 1) git reset HEAD^ <filename of file(s) which need to be removed>

Step 2) git checkout <filename of file(s) which need to be removed>

Step 3) Git commit –amend


  1. Reverting the Commit

The Git revert command undoes a committed snapshot. But, instead of removing the commit from the project history, it figures out how to undo the changes introduced by the commit and appends a new commit with the resulting content. This prevents Git from losing history. We can use the following command:

git revert <commit>



  • This requires our working tree to be clean (no modifications from the HEAD commit).
  • Git revert is used to record some new commits to reverse the effect of some earlier commits (often only a faulty one). If you want to throw away all uncommitted changes in your working directory, you should see git-reset, particularly the –hard option.


  1. Recovery of deleted commits

This includes things like deleting a commit (lets say using git reset hard) on your work branch and then realizing you didn’t mean to delete it.

Well, guess what? You’re in luck! Git doesn’t throw away your commits! And if you’ve accidentally or INTENTIONALLY deleted some work and then realized you want to recover it,  you can with help of below commands:

step 1) git reflog

step 2) git log -g

step 3) git branch recover-branch <commit id from git reflog>

Example is as shown below:


  1. Undo Last Commit

Sometimes in Git we accidentally commit something. This might be because of using ‘git commit -am’ which will include all of the changed files in the new commit. Now how do we undo this commit?

Fortunately, in Git it’s easy to undo last commit (as long as you haven’t pushed it to any remotes yet). Just one command is needed to revert the last commit to the local repository:

git reset HEAD~1

In the above example you can see the accidental commit, then undoing the commit using command ‘git reset HEAD~1’ and finally confirming that the committed file has been reverted by checking the git status. This is to confirm that the file in the commit has been reverted and is outside of the staging area. After this, the commit is no longer included in the git log and we are free to make further changes to it (and others) and do a new commit as normal when ready.

Continue reading…Part III, Git Tips & Tricks (Additional useful commands)


Comments are closed.

About the Writer

  • Tushar Bende
    Senior Software Developer at Synerzip

How can Synerzip Help You?

By partnering with Synerzip, clients rapidly scale their engineering team, decrease time to market and save at least 50 percent with our Agile development teams in India.