The Git Revert command reverts an existing commit by creating a new commit with the exact opposite effect of that existing commit. In other words, git revert records new changes that are opposite to previous changes.
git revert is another way to undo changes in Git. Unlike other undo commands, git revert doesn't remove the commit from your project history. It preserves the history, which is often very helpful for effective collaboration in a team. This lesson discusses how to use git revert on commits.
When To Use Git Revert
git revert is useful when you're fixing bugs in a project. For example, imagine you’re tracking down a bug and discover that the bug was introduced after a specific new commit. Instead of manually writing code to fix it, you can use git revert to undo the changes that introduced it.
Now think of another example. Imagine you are working on a file and made and committed some changes. Later, you worked on another file and also committed those changes. After that, you realize that you made a mistake in your first file, but it happened three commits ago!
You don't want to undo all commits, nor do you want to manually fix the mistake. So how can you do it?
You can use git revert and pass the commit hash that you want to change to the command. Nice!
Note: You couldn't run the git reset command here because it would remove all other changes you made after that commit that you want to change.
As you can see, git revert can be a useful tool in your Git tool belt. But nothing works better than giving it a try yourself!
Git Revert Practice
The git revert command requires a bit of Git history, so you can practice using it with the [Git Practice Repo]((https://github.com/rdesmond/git_practice_repo).
Clone a Practice Repo
- Log in to your GitHub account
- Clone the [Git Practice Repo]((https://github.com/rdesmond/git_practice_repo) to your local machine
- Make some changes to the file called
hungry.txt - Add and commit the file changes with a commit message, e.g.
"icecream" - Run
git logto see the commit history of the repository, including the change you just made to thehungry.txtfile, e.g.:
Git Revert to a Previous Commit
- You're all set to revert a commit! Next, revert the commit with the
"icecream"commit message. To do so, you need to copy the commit hash of that specific commit, then run thegit revertcommand like below:
$ git revert 1d17af2ee2f38757c60058e4ff4474fbb817077b
- Your default text editor will pop up, showing the default commit message when running the
git revertcommand. Here you can change the commit message or just leave it as the default:
- After you save and exit your text editor, you have successfully used Git to revert to the previous commit. To verify that it worked as expected, run
git logagain. Your changelog should look similar to this:
- As you can see, a new commit has been added to your Git history, and the changes recorded there reverted the changes made in the previous commit.
Note: You can give multiple commit-hashes, if you want to revert multiple commits altogether, separated by a space.
- After going through the default process of reverting a commit, you can also look at some different options that the Git Revert commit command comes with, and further customize how you interact with the command.
Git Revert Commit Options
If you run git revert without passing anything else, Git will show you the usage of the possible options:
$ git revert
Edit: -e or --edit
The -e option is used to edit the commit message before reverting the commit:
$ git revert -e <commit-hash>
This is also set as a default when you run the git revert command, so even if you don't pass -e, Git will bring up your text editor to allow you to edit the default commit message it created.
No Edit: --no-edit
The --no-edit option is the opposite of the -e option. If you pass the --no-edit flag, git revert will not open your text editor to edit the commit message. Instead, it will directly revert the specified commit:
$ git revert --no-edit <commit-hash>
The command will apply a default commit message for this new revert commit, which looks like the following:
Revert "<your commit message of the reverted commit>"
This reverts commit <commit hash>.
No Commit: -n or --no-commit
The -n or --no-commit option will prevent git revert from creating a new commit automatically. Instead, Git will revert the changes of the commit you pass, just like it does otherwise, and put them into your staging area. Then you need to commit the changes explicitly using git commit.
$ git revert -n <commit-hash>
Instead of creating the latest commit for you, this option will add the inverted changes to the staging area and your working directory, and then let you do the commit by yourself.
For more information on git revert and its options, read the Git Revert documentation.
Summary: What is Git Revert?
- Git Revert is a way to undo changes in Git. It reverts an existing commit by creating a new commit with opposite changes to the commit you reverted.
- Unlike other undo commands,
git revertretains the reverted commit in your project history. - Use case for Git Revert Commit: You’re tracking down a bug and discover the bug was introduced after a specific commit. Instead of manually writing code to fix the bug, use
git revertto undo the changes that introduced it.
Git Revert Options
- Edit:
-eor--edit - No Edit:
--no-edit - No Commit:
-n/--no-commit