git revert (Git & Dev Tools)
Learn git revert (Git & Dev Tools) step by step with clear examples and exercises.
Why This Matters
In collaborative development projects, it's common for team members to make and update changes in a Git repository. However, these changes may sometimes conflict or create unexpected issues. In such cases, git revert is an essential tool that allows developers to undo specific changes made by themselves or others, ensuring a clean and functional codebase for the project.
By using git revert, you can maintain the integrity of your Git history while fixing errors and conflicts in your project. This helps to keep your project organized and easy to understand for both you and other developers who may work on it in the future.
Prerequisites
To fully understand this guide, you should have a basic understanding of Git and its commands, such as git clone, git add, git commit, git pull, and git status. Familiarity with branching and merging would be beneficial but is not strictly necessary.
Basic Git Concepts
- Git Commands: These are the essential Git commands that every developer should know, including
git clone,git add,git commit,git pull, andgit status. - Branches: Branches allow you to work on different features or changes without affecting the main codebase. You can create, switch between, and merge branches using Git commands.
- Merging: Merging combines changes from two or more branches into a single branch. This is useful for integrating changes made in separate branches back into the main codebase.
Core Concept
What is git revert?
git revert is a command that allows you to undo changes made in your Git repository by creating new commits that apply the inverse of the original commits. This means that instead of deleting the offending commits, git revert creates new commits that reverse the changes made in those commits.
Basic git revert syntax
The basic syntax for using git revert is as follows:
git revert <commit-hash>
Replace ` with the hash of the commit you want to undo. You can find the commit hashes by running git log`.
git revert example
Let's consider a simple scenario where we have made two commits: one adding a new feature, and another introducing a bug. We want to undo the bug-introducing commit while keeping the new feature.
Initial repository state
$ git init
Initialized empty Git repository in /path/to/repo/.git/
Create and add a file with a new feature
$ echo "Feature: Add new functionality" > README.md
$ git add .
$ git commit -m "Add new feature"
[master (root-commit) 0123456] Add new feature
1 file changed, 1 insertion(+)
Introduce a bug in the same branch
$ echo "Bug: Broken functionality" >> README.md
$ git add .
$ git commit -m "Introduce bug"
[master 6543210] Introduce bug
1 file changed, 1 insertion(+)
Undo the bug-introducing commit using git revert
$ git revert 6543210
Stopped at 6543210... Introduce bug
You can amend the commit now, with
git commit --amend
If you wish to proceed, enter 'e' or press Enter to continue.
Amend and save the commit message
$ git commit --amend
[master 9876543] Revert: Introduce bug (bug-fixing commit)
This is a temporary commit that undoes the changes made in the original "Introduce bug" commit.
Now let's clean up by squashing the temporary commit and the original bug-introducing commit into one
$ git rebase -i HEAD~2
In the editor, change 'pick' for the temporary commit to 'squash'
Save and exit the editor
The temporary commit and the original bug-introducing commit are now squashed into a single commit that undoes the bug
$ git log --oneline
0123456 Add new feature
9876543 Revert: Introduce bug (bug-fixing commit)
### git revert with multiple commits
You can also use `git revert` to undo multiple commits by specifying a range of commit hashes. For example, to undo the last three commits, you would use:
git revert ..
or
git revert HEAD~3..HEAD
Worked Example
In this worked example, we'll demonstrate how to use git revert to undo changes made in a collaborative project. Let's assume that you and your teammate have been working on a project, and your teammate has introduced a bug in the latest commit:
Your teammate introduces a bug in the latest commit
$ git push origin master
Counting objects: 5, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 329 bytes | 329.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/username/repo.git
5f67891..8765432 master -> master
You notice the bug and want to undo the commit using git revert
$ git checkout master
Switched to branch 'master'
$ git log --oneline
5f67891 Add new feature
8765432 Introduce bug (bug-introducing commit)
Undo the bug-introducing commit using git revert
$ git revert 8765432
Stopped at 8765432... Introduce bug
You can amend the commit now, with
git commit --amend
If you wish to proceed, enter 'e' or press Enter to continue.
Amend and save the commit message
$ git commit --amend
[master 9876543] Revert: Introduce bug (bug-fixing commit)
This is a temporary commit that undoes the changes made in the original "Introduce bug" commit.
Now let's clean up by squashing the temporary commit and the original bug-introducing commit into one
$ git rebase -i HEAD~2
In the editor, change 'pick' for the temporary commit to 'squash'
Save and exit the editor
The temporary commit and the original bug-introducing commit are now squashed into a single commit that undoes the bug
$ git log --oneline
5f67891 Add new feature
9876543 Revert: Introduce bug (bug-fixing commit)
Common Mistakes
1. Not specifying the correct commit hash
Ensure that you provide the correct commit hash when using git revert. Incorrect commit hashes will result in errors or unexpected results.
2. Failing to amend and save the commit message
When using git revert, you'll be prompted to amend and save the commit message for the temporary commit created by git revert. Failure to do so may result in an uncommitted, incomplete revert.
3. Not cleaning up after the revert
After using git revert, it's essential to clean up by squashing the temporary commit and the original offending commit into a single commit. This step is crucial for maintaining a clean Git history.
4. Creating unnecessary merge commits
When using git revert on multiple commits, ensure that you don't create unnecessary merge commits during the cleanup process. To avoid this, use git rebase -i HEAD~n, where n is the number of commits you want to squash, and change all 'pick' commands to 'squash'.
5. Not merging after a successful revert
If you are working on a feature branch and revert changes on that branch, don't forget to merge your branch back into the main branch (usually master or main) to ensure that your changes are incorporated into the main codebase.
Practice Questions
- You have made several commits in your Git repository, but you want to undo the last three commits. What command would you use?
git revert HEAD~3..HEAD
or
git revert <commit-hash~3>..<commit-hash>
- Your teammate has introduced a bug in the latest commit of a collaborative project. How can you use
git revertto undo the changes made by that commit?
$ git checkout master
$ git revert <commit-hash>
Stopped at <commit-hash>... Introduce bug
You can amend the commit now, with
git commit --amend
If you wish to proceed, enter 'e' or press Enter to continue.
Amend and save the commit message
$ git commit --amend
[master 9876543] Revert: Introduce bug (bug-fixing commit)
Now let's clean up by squashing the temporary commit and the original bug-introducing commit into one
$ git rebase -i HEAD~2
3. You have made changes on a feature branch, but you want to undo those changes before merging them into the main branch. How can you use `git revert` for this purpose?
Create and commit your changes on a new branch
$ git checkout -b my-feature-branch
$ echo "My feature changes" > README.md
$ git add .
$ git commit -m "Add feature changes"
[my-feature-branch 0123456] Add feature changes
1 file changed, 1 insertion(+)
Undo the changes on your feature branch using git revert
$ git checkout my-feature-branch
$ git revert HEAD
Stopped at HEAD: (refs/heads/my-feature-branch) Add feature changes
You can amend the commit now, with
git commit --amend
If you wish to proceed, enter 'e' or press Enter to continue.
Amend and save the commit message
$ git commit --amend
[my-feature-branch 9876543] Revert: Add feature changes (reverting back to initial state)
Now your feature branch is in its original state, and you can make new changes or merge it into the main branch as needed.
FAQ
Q: What is the difference between git reset and git revert?
A: Both commands allow you to undo changes in your Git repository, but they work differently. git reset removes commits from the project history, while git revert creates new commits that reverse the changes made in the original commits.
Q: Can I use git revert to undo changes made by another user?
A: Yes, you can use git revert to undo changes made by another user as long as you have access to their commits. You'll need their commit hash or the branch they pushed to.
Q: Can I undo a merge commit using git revert?
A: No, you cannot directly use git revert on a merge commit. Instead, you can use git revert on one of the parent commits that were merged, or consider using git cherry-pick to selectively undo changes from specific commits in the merge.
Q: How do I find the commit hash for a specific change?
A: You can find the commit hashes by running git log. To narrow down the results, you can use filters such as --author, --grep, or --since to find commits made by a specific author, containing specific text, or after a certain date, respectively.