How to checkout a specific version of git repository

Last updated on October 10, 2020 by Dan Nanni

A public git repository is often shared by multiple developers who constantly contribute new code and fixes, and users who check out code to try it. Along with code itself, a git repository contains full commit history of the code, as well as revision tracking information. As such, you can easily checkout a particular version of specific files from git repository if you want. In order to checkout a specific version of git repository, see the following examples.

I assume that you have already cloned a git repository as follows.

$ git clone <url of git repository>
$ cd <cloned-directory>

Before checking out a specific version, you probably want to examine the change history first.

To view the commit history of the current directory:

$ git log

To view the commit history of a specific file:

$ git log -p src/import.c

The sample commit history looks as follows.

commit 7a51831cb4dc6b2ae56ad24400ba1fdfed064528
Merge: 69f8529 6fd2611
Author: Peter 
Date:   Wed Nov 14 21:58:47 2012 +0000


    Merge "Fixed a critical bug"

The history shows you, for each commit entry, detailed information about the commit, as well as 40-character checksum hash (e.g., 7a518xxxxxx). The checksum hash is essentially a version number you need to remember in order to checkout next.

To checkout a specific version of the current directory:

$ git checkout <checksum_hash>

To checkout a specific version of a file:

$ git checkout <checksum_hash> <file>

Support Xmodulo

This website is made possible by minimal ads and your gracious donation via PayPal or credit card

Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.

Xmodulo © 2021 ‒ AboutWrite for UsFeed ‒ Powered by DigitalOcean