In the post Undoing Changes in Git: restore, reset, revert, we learned that Git provides multiple ways to undo changes: restore, reset, and revert.
In the post Undoing Commits with git reset, we practiced git reset, which can rewrite commit history (safe only for local commits that haven’t been pushed).
In this post, we will practice using git revert—the safest way to undo changes when commits may already be shared with others. Unlike reset, revert does not delete or rewrite history. Instead, it creates a new commit that reverses the effect of an earlier commit.
Before We Begin
Run the commands in this guide inside a terminal and within a Git repository folder.
- Windows: Open Git Bash, PowerShell, or Windows Terminal
- macOS / Linux: Open the Terminal
1. When Do We Use git revert?
We use git revert when:
- A commit is already pushed to a shared remote (like
main) - Others may already have pulled that commit
- We want to undo changes without rewriting history
Important idea:
git revertcreates a new commit that undoes a previous commit.
The original commit remains in history.
2. Setting Up a Practice Repository
Let’s create a repository and make a few commits.
mkdir revert-demo
cd revert-demo
git init
Create a file:
echo "Line 1" > notes.txt
git add notes.txt
git commit -m "Add line 1"
Add two more commits:
echo "Line 2" >> notes.txt
git add notes.txt
git commit -m "Add line 2"
echo "Line 3" >> notes.txt
git add notes.txt
git commit -m "Add line 3"
Now check history:
git log --oneline
You should see three commits.
3. Scenario 1 — Revert the Most Recent Commit
Suppose the last commit (“Add line 3”) was a mistake.
Revert the latest commit:
git revert HEAD
Git may open an editor to confirm the revert commit message. Save and close it to continue (usually it auto-fills a message like “Revert ‘Add line 3’”).
Now check the file:
cat notes.txt
You should see that Line 3 is removed, but Line 1 and Line 2 remain.
Check the commit history:
git log --oneline
You will now see an additional commit at the top: a revert commit.
Key learning:
- The original “Add line 3” commit is still in history
- A new commit was created to undo it
4. Scenario 2 — Revert a Specific Commit (Not the Latest)
Let’s introduce a new mistake as a separate commit so we can revert something in the middle.
Add a “bad” line:
echo "BAD LINE" >> notes.txt
git add notes.txt
git commit -m "Add bad line"
Now add a good commit after it:
echo "Line 4" >> notes.txt
git add notes.txt
git commit -m "Add line 4"
History now has the bad commit followed by a good commit.
Get the commit ID list:
git log --oneline
Copy the commit ID of “Add bad line” and revert it:
git revert <commit-id>
Check the file:
cat notes.txt
The BAD LINE should be removed, while later commits (like “Line 4”) remain.
That’s the power of revert: it works safely even when the bad commit is not the latest.
5. Scenario 3 — Reverting Multiple Commits
Sometimes you need to undo multiple commits.
First list commits:
git log --oneline
You can revert commits one by one:
git revert <commit-id-1>
git revert <commit-id-2>
Note: each revert creates a new commit.
6. Scenario 4 — Revert Without Immediately Committing (Optional)
If you want to review the changes before creating the revert commit, use --no-commit:
git revert --no-commit <commit-id>
Now the reverse-changes are applied, but not committed yet.
Check:
git status
git diff
If everything looks good, commit it:
git commit -m "Revert changes from <commit-id>"
This approach is useful when you’re reverting multiple commits and want to create a single combined revert commit (advanced workflow, but useful in real projects).
7. What If a Revert Causes Conflicts?
Yes—git revert can sometimes create merge conflicts, especially if the code has changed a lot since the original commit.
If a conflict occurs:
- Git will pause and show conflicted files in
git status - Open the files and resolve conflict markers
- Stage the resolved files:
git add .
- Continue the revert:
git revert --continue
If you decide you don’t want to continue the revert:
git revert --abort
8. Quick Command Reference
Revert the latest commit:
git revert HEAD
Revert a specific commit:
git revert <commit-id>
Revert but don’t commit immediately:
git revert --no-commit <commit-id>
Continue after conflict resolution:
git revert --continue
Abort the revert:
git revert --abort
9. Summary
git revert is the safest way to undo a commit when working on shared branches.
- It does not rewrite history
- It creates a new commit that reverses an earlier one
- It works even if the “bad” commit is not the latest
- It is safe for public branches like
main
Quick decision reminder:
- Not committed yet →
git restore - Committed but not pushed →
git reset - Committed and possibly pushed/shared →
git revert