Revert
It is used to undo the changes introduced by a specific commit by creating a new commit that reverses those changes. Unlike git reset, which can alter commit history, git revert is safe for public repositories and works by adding a new commit that undoes the effect of an earlier commit, leaving the history intact.
Why Use git revert?
-
Preserve history: It doesn’t rewrite history, unlike git reset. Instead, it creates a new commit that effectively undoes previous changes.
-
Safe for shared branches: Since it doesn’t alter the commit history, it is useful when working on shared branches with others.
-
Granular undoing: You can undo specific commits in a branch without affecting others.
Example
Let’s assume you have the following commit history in your main branch:
A --- B --- C --- D (main)
Where:
A → B → C → Dare commits in the history.- Commit
Dhas a bug that you need to undo.
Now, instead of resetting to C and rewriting history, you can revert commit D to create a new commit that undoes the changes in D.
Steps
1. Revert a Single Commit
Suppose the commit hash of D is abcd1234. To revert the changes in commit D, you run:
git revert abcd1234
What happens now:
- Git creates a new commit (
E) that undoes the changes introduced byD. - Your new commit history will look like this:
A --- B --- C --- D --- E (main)
Where:
Eis the new commit that undoes the changes made inD.
2. Revert with Conflict Resolution
Sometimes, reverting a commit can cause conflicts (e.g., if the changes in D conflict with the code in later commits). Git will prompt you to resolve the conflicts manually:
-
Resolve the conflicts in the conflicted files.
-
Stage the resolved files:
git add <file1> <file2>
- Complete the revert process:
git add <file1> <file2>
3. Revert Multiple Commits
To revert multiple commits, you can use a range of commits. For example, if you want to revert commits C and D:
git revert C^..D
This will revert all changes from commit C through D inclusively.
Revert and Skip Commit (if needed)
If you don't want to revert a specific commit in the middle of a series of commits, you can use the --skip option:
git revert --skip
This will skip over a problematic commit during the revert process, leaving it unchanged.
Commands
git revert <commit>– Creates a new commit that undoes the changes from the specified commit.git revert HEAD– Reverts the latest commit.git revert HEAD~n– Reverts the commit that is n commits before the latest commit.git revert --no-commit <commit> (-n)– Applies changes from the commit but does not create a new commit.git revert --no-edit <commit>– Reverts a commit without opening the commit message editor.git revert --continue– Continues the revert process after resolving conflicts.git revert --abort– Aborts the revert operation if conflicts arise.git revert -m <parent-number> <merge-commit>– Reverts a merge commit by specifying which parent to keep.
git revert VS git reset
| Feature | git revert | git reset |
|---|---|---|
| History | Adds a new commit that undoes changes | Removes commits or changes the history |
| Commit History | Leaves commit history unchanged | Rewrites history (may require --force) |
| Use Case | Undo a commit safely (on public branches) | Undo recent commits locally |