git checkout specific commit

How to Checkout Specific Commit in Git

Git is a popular distributed version control system used by many developers and organizations around the world. It keeps track of changes to source code in the form of snapshots, known as commits. Each commit has a specific commit ID. These commits allow you to view the changes made in a specific snapshot and even revert to a specific commit. Sometimes you may need to checkout a specific commit. In other words, you may want to switch to a specific commit. In this article, we will learn how to checkout specific commit in Git.


How to Checkout Specific Commit in Git

Here are the steps to checkout specific commit in git.


1. Clone Repository

If you already have a working git repository on your system, you can skip this step. Else you can clone your git repository from a remote URL. We have used a remote URL from GitHub.

$ git clone https://github.com/fedingo/hello-world

You can also clone your git repository from other sites like BitBucket.


2. Go to the Repo Folder

Next, navigate to the repository folder using cd command.

$ cd hello-world


3. View Commits

Next, run the following command to view the commit history of your git repository.

$ git log

Here is a sample output.

origin/HEAD)
Author: John Doe <john.doe@gmail.com>
Date:   Sat Jan 22 19:46:34 2022 +0530

    added 1 user for motory

commit 7d6a76ad9c370ccacabca27d765a8a13ddad36a5
Author: John Doe <john.doe@gmail.com>
Date:   Fri Jan 21 16:33:19 2022 +0530

    updated breadcrumbs

commit 76377b88dec74213da1829e425eee4954df8069b
Author: John Doe <john.doe@gmail.com>
Date:   Fri Jan 21 15:37:40 2022 +0530

    added breadcrumbs for 30 pages

commit 7e843cd23c10cd8088cdf86a3b6081adc86622cc
Author: John Doe <john.doe@gmail.com>
Date:   Fri Jan 21 15:02:29 2022 +0530

The above output lists 3-4 most recent commits, each having a commit ID, author, datetime and the commit message.

Commit history is a history of all the commits made to your git repository, starting from initial commit all the way to the latest one.

Each commit in git history has an SHA1 identifier that is used to uniquely identify the commit. These identifiers are generated and stored by git command.


4. Git Checkout Specific Commit

As you can see in the previous step, the latest commit has an ID 7d6a76ad9c370ccacabca27d765a8a13ddad36a5. If you want to checkout a previous commit with ID, run the git checkout command followed by that commit’s ID.

$ git checkout 7e843cd23c10cd8088cdf86a3b6081adc86622cc

Now the above commit will be checked out for you and its changes will be the latest changes for you.

At this point, you can choose to create a new branch for future commits, using the following command.

$ git switch -c <new-branch-name>

or you can run the following command, to switch back to the latest HEAD (that is the latest commit before checkout).

$ git switch -

But please note, if you checkout a past commit, then your HEAD pointer will become detached, that is, all future changes will not belong to any branch unless you create a new one to record future changes. That is why, git automatically gives you two options shown above – either create a new branch for future changes, or revert back to the original HEAD and then make changes. So, if you have checked out a past specific commit, then please don’t make any changes to your repo unless you create a new branch for them or you switch back to the latest commit HEAD.

Also read:

How to Take Screenshot of Div in JavaScript
How to Create Incremental Backup in Linux
How to Improve Ubuntu Speed & Performance
How to Embed PDF in HTML
How to Run Multiple Python Files

Leave a Reply

Your email address will not be published.