Git Commands Recap : Undo commit (Reset and Revert), Difference between Checkout and hard rest

Git Revert: git revert <SHA-of-commit-to-revert>

Suppose we added a piece of code in given commit and we want to remove it from commit. Git revert comes for rescue.
If Git reverts given commit , then Git will make a new commit where that piece of code is deleted. It also works the other way where if piece of code were removed in given commit, then reverting that commit will add that content back.
➜  custom_cloned_repo git:(master) git revert 582d67c9b0a9376101a50d02f46ee34e0de69772
[master fecb359] Revert "created css"
 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 front.css

Below image shows, a css file is created and committed. Later we find SHA value using "git log" and revert that commit. In reverting that css file is deleted, git does it by creating new commit. 999999999

Git Reset: git reset <--flag/file/sha>

Reverting creates a new commit that reverts or undos a previous commit. Resetting, on the other hand, erases commits. Three flavour of rest command.
  •  Move HEAD : "reset command" moves the branch that HEAD is pointing to. It means if HEAD is set to the master branch (i.e. We are currently on the master branch), running
    git reset 9e5e6a4 will start by making master point to 9e5e6a4.
    ➜  custom_cloned_repo git:(master) git reset --soft Head~
     Below image shows, we have two branch test and master at two different commit. Once we execute above reset command with flag "--soft", head moves to same commit as test branch.

  • Updating the Index (--mixed) : This is default flag associated with reset, if noting is provided.
    reset with --mixed will bring Head and Index at same commit level.
    ➜  custom_cloned_repo git:(master) git reset HEAD~
    ➜  custom_cloned_repo git:(master) git reset --mixed Head~
    On executing reset command with --mixed, master and test branch will be on different commit. Index and Head are on same commit. Below image shows master moves back one commit compare to test.

  • Updating the Working Directory (--hard): Think for a sec before using flag "--hard". This flag will overwrite working directory file with the commit to be reset(Head~ in above example).
    After executing --mixed command, master and test branch were on different commit, see first part of below diagram.
    Now we checkout test and use "--hard" flag, css file is deleted from working directory of test branch because Head~ refers to.

Git checkout <branch> vs git reset --hard <branch>

Running git checkout [branch] is pretty similar to running git reset --hard [branch] in that it updates all three trees(Head, Index and working directory) for you to look like [branch], however there are two important differences.

First, unlike reset --hard, checkout is working-directory safe; it will check to make sure it’s not blowing away files that have changes to them. Actually, it’s a bit smarter than that — it tries to do a trivial merge in the Working Directory, so all of the files you haven’t changed will be updated.
reset --hard, on the other hand, will simply replace everything across the board without checking.

The second important difference is how checkout updates HEAD. Whereas reset will move the branch that HEAD points to, checkout will move HEAD itself to point to another branch.

Below image from Consider master and develop branches which point at different commits and we’re currently on develop (so HEAD points to it). If we run git reset master, develop itself will now point to the same commit that master does. If we instead run git checkout master, develop does not move, HEAD itself does. HEAD will now point to master.
Difference between checkout and reset (Source:

Ancestry References:

To tell Git about the commit that's one before the current commit or two before the current commit. These are relative references, in git we use special special characters called "Ancestry References" to refer relative references. Those characters are:

^ – indicates the parent commit
~ – indicates the first parent commit

The parent commit – the following indicate the parent commit of the current commit

  • HEAD^
  • HEAD~
  • HEAD~1

The grandparent commit – the following indicate the grandparent commit of the current commit

  • HEAD^^
  • HEAD~2

The great-grandparent commit – the following indicate the great-grandparent commit of the current commit

  • HEAD^^^
  • HEAD~3

The main difference between the ^ and the ~ is evident when a commit is created from a merge. A merge commit has two parents. With a merge commit, the ^ reference is used to indicate the first parent of the commit while ^2 indicates the second parent. The first parent is the branch you were on when you ran git merge while the second parent is the branch that was merged in.



  1. If you are a good looking and want to grow yourself with good wealth in the term of money work as a Female Escorts Job in Delhi, then you are welcome in our Call Girls job in Delhi agency, to start your work as Female escorts. We are now hiring a good and decent Charming Call Girls for Escorts Girl Job in Delhi,

    Part time or Full time Escorts Job in Delhi, first of all u have to send your profile. We are the oldest and genuine Female escort’s agency who provide and hiring girls for Female Escorts Job in Delhi in all over India and we never disclose your identity and your protection is our responsibility so just go ahead and join us for the splendid time work as a Call Girls job in Delhi. we have also Escorts Girl Job in Delhi openings.

Previous Post Next Post