February 21, 2025
O. Wolfson
git revert is a powerful command used in Git to undo changes in a repository while maintaining a clear and safe history. Unlike git reset, which rewinds the history and can lead to conflicts in collaborative environments, git revert creates a new commit that undoes changes from a specified commit. This makes it a safer option when working with shared repositories.
This article explores how git revert works, when to use it, when it may not be the best tool, and alternative approaches for modifying commit history.
git revert WorksThe git revert command undoes changes from a specific commit by generating a new commit that reverses the changes. This ensures that the repository history remains intact and avoids issues when collaborating with others.
shgit revert <commit-hash>
<commit-hash>: The identifier of the commit you want to revert.-m or -n flags.git revertWhile git revert is useful, it is not always the right tool for the job. The best use cases include:
git revert does not rewrite history, it allows you to remove changes while keeping the commit log intact.git revert can selectively remove the problem.git revert allows you to roll back the merge without disturbing other commits.git revert Is Not the Right Toolgit revert is not the best choice in situations where:
git reset or git rebase instead).If you don’t already have a Git repository, create one:
shgit init git-revert-demo
cd git-revert-demo
Create a new file and add some text to it:
shecho "Initial version" > example.txt
Stage and commit the file:
shgit add example.txt
git commit -m "Initial commit"
Modify the file and make a second commit:
shecho "Added some changes" >> example.txt
git add example.txt
git commit -m "Added changes"
Make another change that introduces an issue:
shecho "Buggy feature added" >> example.txt
git add example.txt
git commit -m "Bug introduced in feature"
Make a final change that is unrelated to the bug:
shecho "Final enhancement" >> example.txt
git add example.txt
git commit -m "Final enhancement"
View the commit history:
shgit log --oneline
Identify the commit hash of the buggy commit (not necessarily the latest one).
Instead of reverting the last commit, revert an older one (e.g., "Bug introduced in feature"):
shgit revert <commit-hash>
Important: If later commits reintroduced similar changes, the revert might not fully remove them. Verify by checking example.txt after the revert.
Check the file content:
shcat example.txt
If the buggy feature is still present (due to later commits preserving it), you may need to manually edit the file and commit the fix:
shgit add example.txt
git commit -m "Manually removed buggy feature after revert"
git revert --no-commitFor more control, use:
shgit revert --no-commit <commit-hash>
This applies the revert without committing, allowing you to manually review and modify the changes before committing.
git rebase -i)If you need to remove or modify multiple past commits, git rebase is often a better choice:
shgit rebase -i <commit-hash>^
Change pick to edit for the commit you want to modify, then manually adjust example.txt and commit the corrected changes.
Reverting commits that affect the same file may lead to conflicts. Resolve them manually and run:
shgit revert --continue
If you mistakenly revert a commit, you can revert the revert:
shgit revert <revert-commit-hash>
This restores the original changes.
git revert is an essential Git tool for safely undoing changes without rewriting history. It is especially useful in collaborative environments where modifying commit history is discouraged. However, it is not always the best choice—especially if later commits have preserved parts of a reverted change.
When to use **git revert**:
✅ Removing a bad commit while keeping history intact.
✅ Undoing a change in a shared branch.
✅ Reverting a merge commit without disturbing others.
When to consider other options:
❌ Removing commits completely (git reset).
❌ Editing multiple past commits (git rebase -i).
❌ Reverting a commit that has been modified by later commits (git revert --no-commit + manual edit).
By understanding when and how to use git revert, you can ensure a clean and well-maintained project history.