When you’re working with a Git repository, you may accidentally create a commit with code that isn’t ready to be stored in your Git repository.
That’s where the git revert command comes in. The git revert command allows you to
undo a commit so that you can return a repository to the previous commit.
This tutorial will discuss, with examples, reverting code and how to use the git revert command to revert your code. By the end of reading this tutorial, you’ll be an expert at reverting code using the git revert commit command.
Reverting Your Code
Sometimes, when you are working with a Git repository, you may notice that you’ve made a mistake in your commit.
For instance, suppose you have just finished working on a new feature, and you’ve realized that there is a bug that needs to be fixed. When you commit the bug fix, you also notice that you have committed the code for the new feature you are developing.
This means that your commit includes both a bug fix and a new feature, which may be confusing for other collaborators on a project to understand. Instead of committing both of these changes at the same time, you wanted to include them in two separate commits.
In this scenario, you may want to revert your repository to the state it was in before you pushed the commit. This will give you another chance to push your commits.
The Git Revert Command
The git revert command allows you to
undo a commit. However, instead of deleting a commit from a project, the git revert command finds the changes between the last two commits and creates a new commit that reverses those changes.
The git revert command is useful because it allows you to preserve the project history of a repository. Rather than deleting a commit entirely, it allows you to revert a repository to another commit, so you can still keep an accurate record of every commit you have pushed to a repository.
You should use the git revert command in situations where you want to reverse your last commit. Instead of manually making the changes you need to make to your last commit, you can revert your commit and push a new one to the codebase.
The syntax for the git revert command is as follows:
Let’s walk through an example to illustrate how this command works.
Git Revert Example
On our local machine, we have an example repository with one file: README.md. We have just made a change to the file that we want to push to a commit. We can do so using this code:
git commit -m "docs: Update README.md with author info"
This command, when executed, creates a commit with the message
docs: Update README.md with author info. We have also just made another change to our README.md file, which we commit to our repo using this command:
git commit -m "docs: Update order of authors by contributions"
This command creates another commit in our repository. If we use the git log –pretty=oneline command (which shows us a short list of our commits), we can see that there are two commits in our repo history:
6f52d877873e7d3b52c929647384dfdf2488da22 (HEAD -> master) docs: Update order of authors by contributions 57d763663e619088159bb7629243456f88feab79 docs: Update README.md with author info
Now, suppose we decide that we want to revert our last commit. We realize that the order in which we wrote the authors in our README.md file was incorrect, and so we need to revert our repository back to the state it was in before we pushed our last commit.
We could do so using this command:
git revert HEAD
When we run this command, our default text editor opens and prompts us to create a message for our commit. In this command, HEAD refers to the latest commit. In our example, we type in the message
revert author order commit. Then, the command returns the following:
[master b66c29a] revert author order commit "docs: Update order of authors by contributions" 1 file changed, 1 deletion(-)
Now, when we run the git log –pretty=oneline command, we can see a new commit has been created:
b66c29a8c4c226fa9ae8cd8f9e086c0a73e6ecfe (HEAD -> master) revert author order commit "docs: Update order of authors by contributions" 6f52d877873e7d3b52c929647384dfdf2488da22 docs: Update order of authors by contributions 57d763663e619088159bb7629243456f88feab79 docs: Update README.md with author info
Our commit history now lists three commits. Instead of deleting our last commit, the git revert command created a new commit that has undone the changes of the previous commit. This means that we still have a complete history of all the commits we pushed to our repository.
The git revert command needs a commit reference to execute. In this case, we specified HEAD, which reverts our repository to the last commit.
If we wanted to revert back to another commit, we could specify the hash value for that commit. So, instead of stating
HEAD in our command, we could use a hash returned by the git log command to revert to a commit of our choosing. Here’s an example the command we would use to revert a repository back to a specific commit:
git revert 57d763663e619088159bb7629243456f88feab79
This command would revert our repository to the commit with the SHA hash
57d763663e619088159bb7629243456f88feab79. In this repository, that hash corresponds with the first commit we made to our repo.
Git Revert vs. Reset
The revert and reset commands are both used to undo changes from earlier commits in a Git repository. However, you should be aware that these commands do have slightly different behaviors.
The git revert command line operation is used to undo the previous commit. When you run git revert, the changes between two commits are undone. Then, a new commit is created which contains the code in your repo after the changes have been undone.
The git reset command, on the other hand, reverts a repository back to its previous state by removing all commits between a certain range. So, if you run git reset and reference a previous commit, all commits between the current state of the repository and that commit will be deleted.
Reverting a commit is often preferred over resetting a commit. The main reason is because git revert does not change the history of your project. This allows you, the developer, to maintain an accurate record of all the changes made to a project.
In addition, the git revert command allows you to revert to an individual commit at a certain point in your repository’s history. The git reset command, on the other hand, only works back from the commit which you are currently on.
This means that, if you wanted to undo a commit far back in the history of your repository, you would have to remove all the commits before that point, then commit your changes. This makes it difficult to preserve an accurate record of the history of your repository, which is a crucial part of effectively using source control tools like Git.
The git revert command allows you to undo the changes you have made to a code repository since a specific commit. Instead of deleting a commit, the git revert command identifies the changes between the current commit and a previous commit and creates a new commit to revert those changes.
This tutorial discussed how to use the git revert command to revert a commit in Git. Now you’re ready to start reverting commits using the git revert commit command like a professional developer!