Go to a particular commit of a git repository with submodules

If you want to go to a particular commit of a git repository with submodules you can use 2 git commands: reset or checkout. You will also need to synchronise the submodules after the working directory has been altered as that doesn’t happen automatically. In this short guide I will show you how to do all that.

Go to a particular commit of a git repository

Sometimes when working on a git repository you want to go back to a specific commit (revision) to have a snapshot of your project at a specific time. To do that all you need it the SHA-1 hash of the commit which you can easily find checking the log with the command:

git log

or any GUI client like gitk. If you want to use the command line I would recommend to use the following options with the log command:

git log --abbrev-commit --pretty=oneline

which will give you a compact list of all the commits and the short version of the SHA-1 hash.

Now that you know the hash of the commit you want to go to you can use one of the following 2 commands:

git checkout HASH


git reset --hard HASH

The main difference between them is that checkout is generally safer as it won’t overwrite local changes in the working directory. Furthermore it won’t alter the current branch you are into.

If you wanted to know more about the difference between git reset and git checkout I would recommend to read Reset Demystified on the official git blog.

Synchronise the submodules

After checking out or resetting the working directory of your project you will probably notice that the directories containing a submodule are marked as “modified” and that they have not been altered (local files are unchanged). To synchronise all your submodules to the version they were at the time of the commit you went back to all you need to do is running a single command:

git submodule update

Going back to the most recent commit

If you want to go back to the most recent version of your project you have to follow 2 different procedures based on what command you before.

In case you used git checkout, you need to use it again like in the following command (assuming you are in the branch master):

git checkout master

In case you used git reset, you need to use:

git pull

In both cases you will still need to update the submodules after that by running:

git submodule update

as previously done.


In case you wanted to learn more about the commands described in this post you can check out the official git reference:


To get updates about my future posts you can follow me on Google+, Linkedin or Twitter.

Feel free to share this post if you find it interesting and or useful. For any question or suggestion leave a comment below.

Leave a Comment

Your email address will not be published. Required fields are marked *