How to Revert a Git Repository by Commit ID

John Wachira Feb 02, 2024
  1. Switch to a Git Commit Temporarily
  2. Revert a Git Repo by Commit ID
How to Revert a Git Repository by Commit ID

This article will discuss reverting a Git repository based on commit id. We may need to go back to an older commit to examine its state or delete the commits after it.

Let’s discuss both scenarios.

Switch to a Git Commit Temporarily

We use the git checkout command to temporarily switch to an older commit while mentioning the commit id.

$ git checkout <Commit ID>

This command will detach our repo’s HEAD (we are not checked out on a branch). In this state, we cannot make commits.

As shown below, we must create a new branch based on the commit.

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

We can combine the above command to do the same thing as shown below.

$ git checkout -b <new-branch-name> <Commit ID>

Revert a Git Repo by Commit ID

We use the git reset command with the --hard flag while passing the commit id we want to roll back to. Run the command below.

$ git reset --hard <Commit ID>

Running this command will delete all our recent commits up to the mentioned commit. The command will also delete any uncommitted changes in your index.

If you want to keep the changes, do this:

$ git stash
$ git reset --hard <Commit ID>
$ git stash pop

If you want to update the remote repo, use the git push command below.

$ git push -f

Be careful with this command, as it will override the remote repo based on your local repo. Let us look at an example.

In the example below, we will attempt to revert our git repo Delftscopetech based on a commit id. Let’s run the git log command to list all commits in our repo.

$ git log --oneline

repo history

How would we go about it if we wanted to return to the Fourth Update?

We run the git reset command in the context below.

$ git reset --hard df90895
HEAD is now at df90895 Fourth Update

The output shows that our HEAD is now at the Fourth Update. We can run the git push command to push the changes to our remote repository.

If you want to undo the reset, follow this.

First, we run the git reflog command to see all reference updates in our repo.

$ git reflog

git reflog

Your output will look similar. We can see our reset at HEAD@{0}.

To go back, we run the git reset command as shown below.

$ git reset HEAD@{1}

Let’s check our commit history now.

$ git log --oneline

repo history 2

Our HEAD is back at Sixth Update.

In a nutshell, it is very easy to revert your Git repo based on commit id. If you like to temporarily switch to a commit, use the git checkout command.

To revert to a specific commit, use the git reset --hard command and indicate the commit hash.

Author: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.


Related Article - Git Reset