How to Revert the Last Commit Locally and Remote in Git

Working as software engineers means constantly switching between repository branches, committing file changes, and pulling and pushing changes in different branches. However, there is a time when you commit local files that should have never been in the repository, and even worst you end up using the push command to add those changes in the remote repository.

What can you do to fix this? Fortunately, Git has options you can use to roll back to the last commit. In this article, we are going to show you how to use the revert command as well as make sure you are reverting to the previous commit in a local and remote environment.

Reverting the Last Commit

We are going to start by learning how to use the git revert command. Then, we will show you how to revert commits in your local repository as well as revert commits in a remote repository.

How Git revert Works

The command revert in Git is used to generate a new commit to reverse the changes made in previous commits.

Explanation of what git revert does

Using the revert command in Git is as simple as providing the commit identifier.

git revert <commit id>

Let’s use the previous diagram as a repository example where we have git commits of A, B, and C, and C is the current commit. In case we want to revert the changes made from B to C, what we need to do is to use commit C to revert to the state of B.

git revert C

Another option is to use the keyword HEAD instead of the commit id. The keyword HEAD is another way to say the current commit. Hence, if the current commit is C, then HEAD will pull the commit id of C.

git revert HEAD

Reverting the Last Commit Locally

  1. Let’s make sure we don’t have any changes in the current branch by using the status command.
git status

You will know there are no changes in your current branch if your terminal does not display the following message:

Changes not staged for commit:

2. Now, we need to get the id of the current commit. To do so, we will use the log command to display the list of previous commits we have made. I recommend using the --oneline flag to get the information of previous commits in one line. Otherwise, it will display other information such as the author and date of each commit, which is not needed to revert the last commit.

git log --oneline

For instance, this is the list of previous commits I have in my current project.

Terminal result from using git log –oneline command

The first commit id logged in the terminal is the current commit where you are now. In my case, the current commit id is 98cfeb4.

3. Use the current commit id or the HEAD keyword if you want to revert the last commit changes.

git revert 98cfeb4

or

git revert HEAD

4. Once you use the revert command, your terminal should display the following message:

This reverts commit 98cfeb4fbd68f65346a764cd2b90c0b4900f7a5f

Git will also prompt you to add a commit message. The default message will start with the text “Revert” followed by the commit message of the commit being reverted.

Git prompting to change commit message after using git revert

Feel free to update the message. In my case, I won’t update it.

5. Type :qa! to exit VIM mode

Exit VIM mode

6. You have reverted the last commit changes in your local repository. One quick way we can verify is by checking a new commit has been generated in the logs. Hence, let’s use again the log command.

git log --oneline

Notice how 98cfeb4 is no longer the current commit in my example.

New commit generated after using git revert

Reverting the Last Commit in Remote

To revert the last commit in a remote repository, you have to follow all the steps to revert the last commit in your local repository. Then, make sure to sync your remote repository using pull and push commands.

For example, if we look at the last image which is the log of the commits in a local repository, you will notice the origin commit that is in sync with the remote repository is commit 98cfeb4.

Commit in sync with the remote repository

Hence, we are going to push the latest commit, which is eca19cb, or commit that reverts changes made on commit 98cfeb4. However, prior to doing so, make sure there to get the latest changes from the remote repository by using the pull command.

git pull

Now, we are ready to push the revert to commit to the remote repository.

git push

To verify the remote is synced with the local repository, you have two options things. One is to use the log command.

git log --oneline

And verify the origin is in the same commit of the HEAD.

Remote synced with local repository

The other option is to check the latest commit in your remote repository matches the latest commit in your local repository. In my case, I used Github.

Checking in Github latest commit matches the latest commit from a local repository

Conclusion

In this article, you learned how to revert changes made on your latest commit using the revert command in both local and remote repositories. While you don’t typically revert changes every day if you are a programmer, it is important to know how to use the revert command in case you made a mistake to commit file changes that you didn’t intend to commit.

Interested in Learning Other Git Commands?

I wrote other articles explaining how to use other git commands, and I thought you might be interested in reading some of them since you are reading this.

Did you like this article?

Share your thoughts by replying on Twitter of Become A Better Programmer or to personal my Twitter account.