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.
Table of Contents
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.
revert in Git is used to generate a new commit to reverse the changes made in previous commits.
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
- Let’s make sure we don’t have any changes in the current branch by using the
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.
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
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.
Feel free to update the message. In my case, I won’t update it.
:qa! to 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
git log --oneline
Notice how 98cfeb4 is no longer the current commit in my example.
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
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.
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
Now, we are ready to push the revert to commit to the remote repository.
To verify the remote is synced with the local repository, you have two options things. One is to use the
git log --oneline
And verify the origin is in the same commit of the HEAD.
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.
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.
- How to Rebase in Git: Explained Step-by-Step
- The Complete Guide to Git Alias: Shorcuts to Be Efficient
- What is Git HEAD? A Practical Guide Explained with Examples
- Learn How to Use Version Control with Git and GitHub: The Absolute Guide for Beginners
- How to Update GitHub Personal Access Tokens?
Did you like this article?