How to Check, Revert and Reset Commits in Git

Sometimes you might need to check, revert or reset a commit in your Git repository. This could be due to a mistake or because you want to review a project as it was at a specific commit. 

 

In this tutorial, we will learn how to check a Git repository history, make a revert and reset a commit.

 

Checkout a Commit

The Git checkout command allows you to go back to a previous commit in read-only mode. This is completely safe as you will not be able to overwrite any of the work in your repository and you will easily be able to go back to the head of the branch.

 

Let's assume you already have made some commits in your repository and would like to checkout one of them. The first step is to list all of the previous commits. We can do this with the Git log command. I am displaying the output of the log on one like using --oneline flag to make things easier to read.

 

git log --oneline
e3308d2 (HEAD -> master) added an about page.
23a9636 Added h1 styling rules.
dd4ff7d Added some text in the title tag

 

In my project, I have three commits and Git is telling me that the head of the branch (the last commit) has the ID of e3308d2.

 

Let's say we wanted to go back to the very first commit and checkout what the code looked like there. We would type git checkout followed by the ID of the commit supplied in the GIt log.

 

git checkout dd4ff7d
Note: switching to 'dd4ff7d'.

 

Now everything in your repository will be changed to the state of the commit you switched to.

 

Reverting Back to the Latest Commit

Once you have finished looking at the project as it was on a previous commit, you can go back to the latest commit using the Get checkout command followed by the name of the branch. This will most likely be master unless you have called it something else.

 

git checkout master
Previous HEAD position was dd4ff7d Added some text in the title tag
Switched to branch 'master'

 

Now you are back to the head of the master branch.

 

Revert a Commit

Git revert undoes one commit in a branch. The contents of the code in your project from the commit will be deleted, but it can be reverted back from within Git. Let's have a look at my repository and use Git revert to remove a commit.

 

git log --oneline
e3308d2 (HEAD -> master) added an about page.
23a9636 Added h1 styling rules.
dd4ff7d Added some text in the title tag

 

Let's say I wanted to remove the commit where I added some h1 styling rules. I would write git revert followed by the ID of the commit.

 

git revert 23a9636
Revert "Added h1 styling rules."
  
This reverts commit 23a9636c76abf259bff7fdc5fbea6f82c23f1ec2.

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# On branch master
# Changes to be committed:
#       deleted:    style/style.css
#

 

You will see a Vim editor pop-up. This just gives you the opportunity to edit the description of the revert, to explain why you reverted the commit.

 

To exit Vim type :wq (write then quit).

 

You will then get an output explaining what changes have been made:

Removing style/style.css
[master 53933ba] Revert "Not happy with styling - added h1 styling rules."
 1 file changed, 8 deletions(-)
 delete mode 100644 style/style.css

 

The Git revert command is actually a commit, but a commit to revert all the changes from another commit. You can see this if you look at the Git log.

 

git log --oneline
53933ba (HEAD -> master) Revert "Not happy with styling - added h1 styling rules."
e3308d2 added an about page.
23a9636 Added h1 styling rules.
dd4ff7d Added some text in the title tag

 

As you can see the original commit we reverted is still there – you could now revert the revert to get its contents back in your code. Don't make a habit of this, however, as your commit history will start to get messy.

 

Reset Commit

The Git reset command will remove the commits from a branch up to (not including) the ID of the one you specify in the command. You will notice that your files/code changes have not been deleted; this gives you the opportunity to add them back into the staging level using git add if you decide you actually want to keep them.

 

Before running get reset my log looked like this:

git log --oneline
e3308d2 added an about page.
23a9636 Added h1 styling rules.
dd4ff7d Added some text in the title tag
git reset 23a9636
git log --oneline
23a9636 (HEAD -> master) Added h1 styling rules.
dd4ff7d Added some text in the title tag

 

When you run the next commit all the code changes omitted by Git reset will be deleted forever.

 

Conclusion

You now know to checkout, revert and reset commits in Git. My advice is that you set up a throw-away repository and have a play about with all the Git features we learned in this tutorial. This will build your confidence to work comfortably with files you care about.