When working with Git, you may need to clone or check out a specific commit to review or use the code at that point in time. Every commit in Git is assigned a unique identifier, called a SHA (Secure Hash Algorithm), which acts as a snapshot of the project’s state at that moment.
By checking out a commit using its SHA, you can view or work with the code exactly as it was during that commit, without affecting the main branch. This is useful when debugging or reviewing past versions.
In this tutorial, we’ll explore how to checkout or clone a repository from a specific Git commit ID (SHA).
Table of Contents
What is a Commit ID?
A commit ID, also known as a SHA (Secure Hash Algorithm), is a unique identifier that Git generates for each commit. This SHA is a 40-character string that ensures the integrity and uniqueness of each commit in the repository. It helps track changes, navigate the project’s history, and manage different versions of the codebase.
Why Use a Specific Commit ID?
There are several reasons you might want to checkout or clone a repository from a specific commit ID:
- Debugging: You can pinpoint when a bug was introduced by checking out the code as it was at various points in history.
- Code Review: Reviewing the code as it was at a particular commit can help understand changes and their impacts.
- Stable Versions: Sometimes, a specific commit represents a stable version of the code that you need to replicate or build upon.
How to Check Out a Specific Commit?
To check out a specific commit in Git, you use the git checkout command along with the commit hash (SHA). Below is a step-by-step guide on how to do it:
1. Open your terminal or command prompt and navigate to the directory of your repository using the cd command:
# cd your-repository
2. Ensure your local repository is up to date with all the commits from the remote repository:
# git fetch --all
3. If you don’t know the commit SHA, you can get it from the log:
# git log
Output:
commit a1b2c3d4e5f6g7h8i9j0 (HEAD -> main, origin/main)
Author: Your Name <you@example.com>
Date: Mon Jun 24 14:00:00 2024 +0000
Initial commit
4. Copy any commit (SHA) ID from the above output and use the git checkout command followed by the commit ID (SHA) to checkout the specific commit:
# git checkout commit-id
For example:
# git checkout a1b2c3d4e5f6g7h8i9j0
Output:
Note: checking out 'a1b2c3d4e5f6g7h8i9j0'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:
git checkout -b new-branch-name
5. After checking out a specific commit, your repository will be in a “detached HEAD” state, which means you are not on any branch. You can still make changes and create new commits when in a detached HEAD state. However, these changes are not associated with any branch.
To save your work, you can create a new branch:
# git checkout -b new-branch-name commit-id
6. Verify that you have successfully checked out the specific commit by looking at the commit log.
# git log -1
This will display the latest commit, matching the commit ID you checked out.
commit a1b2c3d4e5f6g7h8i9j0 (HEAD -> my-new-branch)
Author: Your Name you@example.com
Date: Mon Jun 24 14:00:00 2024 +0000
Initial commit
How to Clone a Repository from a Specific Commit?
Cloning a Git repository from a specific commit allows you to work with the codebase at that exact point in history. Follow the below steps to clone a repository from a specific commit:
1. First, clone the repository as usual:
# git clone repository-url
For example:
# git clone https://github.com/user/repo.git
2. Change the directory to the newly cloned repository:
# cd repo
3. Use the git reset –hard command to reset your working directory to the specific commit. Replace COMMIT-SHA-ID with the actual commit hash.
# git reset --hard COMMIT-SHA-ID
For example:
# git reset --hard a1b2c3d4e5f6g7h8i9j0
Output:
HEAD is now at a1b2c3d4e5f6g7h8i9j0 Initial commit
4. After resetting to the specific commit, you can pull the latest changes to update your references.
# git pull
5. Perform another hard reset to the specific commit to ensure your working directory and index are in the exact state of that commit.
# git reset --hard a1b2c3d4e5f6g7h8i9j0
Output:
HEAD is now at a1b2c3d4e5f6g7h8i9j0 Initial commit
Conclusion
In this article, we’ve covered the essentials of checking out and cloning a repository from a specific Git commit ID (SHA). We explored what a commit ID is and why it might be useful, and we provided step-by-step instructions on how to achieve this task.
Whether you’re debugging, reviewing code, or working with stable versions, knowing how to navigate and manage specific commits is a valuable skill in any developer’s toolkit.
FAQs
1. Can I make changes after checking out a specific commit?
Yes, you can make changes and commit them, but these commits will not belong to any branch unless you create a new branch.
2. What does 'git reset --hard' do?
This command resets the working directory and the index to match a specific commit. Be cautious, any uncommitted changes will be lost.
3. Can I push changes from a specific commit checkout?
Yes, but only if you create a branch from that commit. Otherwise, the changes are in a detached HEAD state and can’t be pushed.
4. How do I get back to the latest commit after checking out a previous one?
You can switch back to the latest commit by checking out your branch again using git checkout main command.





