How to revert a Git commit: A simple example
The git revert command is commonly misunderstood. In this quick tutorial, we will show you exactly how the command works and how to perform a simple undo in your repo.
The most misunderstood operation in the world of distributed version control must be the git revert command. Let's walk through an example of how to revert a Git commit, and differentiate the git reset and git revert commands.
The purpose of the git revert command is to remove all the changes a single commit made to your source code repository. For example, if a past commit added a file named index.html to the repo, a git revert on that commit will remove the index.html file from the repo. If a past commit added a new line of code to a Java file, a git revert on that commit will remove the added line.
When you revert a Git commit, the changes from the targeted commit are removed from your local workspace. A new commit is also created to reflect the new state of your repository.
The git revert command
The syntax to revert a Git commit and undo unwanted changes is simple. All developers need to do is issue the git revert command and provide the ID of the commit to undo:
git@commit /c/revert example/ $ git revert 4945db2
A git revert commit example
To really understand how to undo Git commits, look at this git revert example.
We will start with a git init command to create a completely clean repository:
git@commit /c/revert example/ $ git init Initialized empty Git repo in C:/git revert example
With the repository initialized, we'll add five files to the repo. Each time a new file is created, we add it to the Git index and create a new commit with a meaningful message.
git@commit /c/revert example/ $ touch alpha.html $ git add . && git commit -m "1st git commit: 1 file" $ touch beta.html $ git add . && git commit -m "2nd git commit: 2 files" $ touch charlie.html $ git add . && git commit -m "3rd git commit: 3 files" $ touch delta.html $ git add . && git commit -m "4th git commit: 4 files" $ touch edison.html $ git add . && git commit -m "5th git commit: 5 files"
A quick directory listing following the initial command batch shows five files in the current folder:
git@commit /c/revert example/ $ ls alpha.html beta.html charlie.html delta.html edison.html
A call to the git reflog command will show us our current commit history:
git@commit /c/revert example/ $ git reflog (HEAD -> master) d846aa8 HEAD@{0}: commit: 5th git commit: 5 files 0c59891 HEAD@{1}: commit: 4th git commit: 4 files 4945db2 HEAD@{2}: commit: 3rd git commit: 3 files defc4eb HEAD@{3}: commit: 2nd git commit: 2 files 2938ee3 HEAD@{4}: commit: 1st git commit: 1 file
How to revert a Git commit
What do you think would happen if we did a git revert on the third commit with ID 4945db2? This was the git commit that added the charlie.html file.
git@commit /c/revert example/ $ git revert 4945db2
Will the git revert of commit 4945db2 remove charlie.html, delta.html and edison.html from the local workspace?
Will the git revert of commit 4945db2 remove delta.html and edison.html from the local workspace, but leave the other files alone?
Or will this git revert example leave four files in the local workspace and remove only the charlie.html file?
If you chose the last outcome, you'd be correct. Here's why.
The git revert command will undo only the changes associated with a specific commit. In this git revert example, the third commit added the charlie.html file. When we revert said Git commit, the only file removed from our repository is charlie.html.
git@commit /c/revert example/ $ ls alpha.html beta.html delta.html edison.html
Developers also need to know that when they git revert a commit, the reverted commit is deleted from their local workspace, but not deleted from the local repository. The code associated with the reverted Git commit remains stored in the repository's history of changes, which means reverted code is still referenceable if it ever needs to be accessed or reviewed in the future.
Compare git revert vs. reset
When you git revert a commit, only the changes associated with that commit are undone. Cumulative changes from subsequent commits aren't affected. If you wish to undo every change since a given commit occurred, you'd want to issue a hard git reset, not revert.
The git reset and revert commands are commonly confused, but they apply to distinct use cases. To undo changes associated with a specific commit, developers should use the git revert command. To undo every change that has happened since a given commit occurred, use git reset. In the specific case where a developer needs to undo only the previous commit, either of the git revert or git reset commands will suffice.
Steps to revert a Git commit
In review, the steps to git revert a commit and undo unwanted changes are the following:
- Locate the ID of the commit to revert with the git log or reflog command.
- Issue the git revert command and provide the commit ID of interest.
- Supply a meaningful Git commit message to describe why the revert was needed.
The git revert command is a simple way to remove a bug introduced to the version control system at some point in the past, or back out of a feature enhancement that wasn't well-received by the client. If you want to undo changes that happened in a specific commit, the git revert command is the correct operation to choose.