How to Revert a Git Repository to a Previous Commit

John Wachira Feb 15, 2024
  1. Temporarily Rollback to a Previous Commit
  2. Delete Unpublished Commits
  3. Undo Published Commits
How to Revert a Git Repository to a Previous Commit

This article outlines how you can revert a Git repository to a previous commit. Here are the three methods we will cover.

  1. Temporarily rollback to a previous commit
  2. Delete unpublished commits
  3. Undo published commits

Temporarily Rollback to a Previous Commit

Our first method involves the use of the git checkout command.

This will allow us to move back to a previous Git commit without rewriting the commit history in our branch. Here is an example.

Assuming this is our commit history, how do we move back to three commits? (i.e. HEAD~3)

commit history

We can run the git checkout command, as illustrated below.

$ git checkout HEAD~3

Detached mode

As seen above, we are currently in detached mode. We can make changes, experiment and commit the changes without impacting any branches.

Take caution while in detached HEAD mode, as you can lose the changes made. To retain these changes, create a new branch, as shown below.

$ git checkout -b Detached

You can name your branch as you please.

Delete Unpublished Commits

Unpublished changes are simply the commits you have not yet pushed to the remote repository.

You can delete the commits to roll back your local repository to a previous state with the git reset command. Here is an example.

What if we wanted to hard delete the three commits we discussed in the section above rather than temporarily switching? How would we go about it?

We will run the git reset command to hard delete the three commits, as illustrated below.

$ git reset --hard HEAD~3

The command above will do away with the three commits and any uncommitted changes. Make sure you run the git stash command if you want to retain your uncommitted changes.

Note that this method should only be used when you have not pushed your changes to the remote repository, especially if it is a shared repo.

Undo Published Commits

In the section above, we have seen how you can delete unpublished commits. What if you have already pushed the commits to the remote repository?

The git reset --hard command is destructive and can mess up your project’s timeline where multiple developers are working on the same project.

The safest way of undoing published commits involves using the git revert command. This does not rewrite the commit history but reverts it and creates a new commit.

It will make it easier for other developers to understand what is happening.

To revert the three commits, we will run:

$ git revert --no-commit HEAD~3..HEAD

revert three commits

The --no-commit flag will allow us to create one commit message for all three reverted commits. Failure to include the flag will force you to create a commit message for each commit which looks messy and will clutter your repository.

In a nutshell, there are three ways of reverting to a previous commit in Git. It all depends on the definition of reverting in your circumstance.

If you want to temporarily switch to a previous commit, use the git checkout command. The git reset --hard should be reserved for unpublished changes, while the git revert command works best for published commits.

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.

LinkedIn

Related Article - Git Revert