Introduction
Git is a powerful version control system that helps developers manage code changes efficiently. One of its useful features is the ability to revert commits---undoing changes from previous commits. The git revert
command is commonly used for this, but the --no-commit
flag adds extra flexibility, especially when reverting multiple commits.
In this guide, we'll explore git revert --no-commit
for beginners, intermediate, and advanced users, with practical examples at each level.
1. Understanding git revert
What is git revert
?
git revert
creates a new commit that undoes the changes from a previous commit. Unlike git reset
, it doesn't delete history---making it safe for shared repositories.
Basic Syntax:
git revert <commit-hash>
This creates a new commit that reverses the changes from <commit-hash>
.
Example:
Suppose we have commits:
A -- B -- C (HEAD)
To revert commit B
:
git revert B
This adds a new commit D
that undoes B
:
A -- B -- C -- D (HEAD)
2. Introducing --no-commit
Why Use --no-commit
?
By default, git revert
auto-commits changes. But sometimes, you want to:
Revert multiple commits at once.
Review changes before committing.
Combine multiple reverts into a single commit.
Syntax:
git revert --no-commit <commit-hash>
This applies the revert but does not commit it.
Example: Reverting Multiple Commits
Given:
A -- B -- C (HEAD)
To revert both B
and C
in one commit:
git revert --no-commit C
git revert --no-commit B
git commit -m "Reverted B and C together"
Now, the history looks like:
A -- B -- C -- D (HEAD)
Where D
undoes both B
and C
.
Pro Tips & Tricks for Efficient Undoing
Using git revert --no-commit
effectively requires more than just knowing the syntax. Here are pro tips and tricks to help you master reverting commits like a Git expert!
1. Reverting a Range of Commits (Without Auto-Commit)
Problem:
You want to undo multiple commits in a range (e.g., commitA
to commitD
), but don't want separate revert commits.
Solution:
Use git revert --no-commit
with a commit range.
Example:
git revert --no-commit oldest-commit^..newest-commit
-
oldest-commit^
ensures the oldest commit is included.
Demo:
# Revert commits B, C, and D in one go
git revert --no-commit B^..D
git commit -m "Reverted commits B, C, and D together"
Result:
- A single commit undoes all changes from
B
toD
.
2. Interactive Revert (Selectively Undoing Changes)
Problem:
You want to revert a commit but keep some changes from it.
Solution:
1. Revert with --no-commit
.
2. Manually edit files before committing.
Example:
git revert --no-commit C
# Check changes with git status
git status
# Manually edit files to keep desired changes
git add .
git commit -m "Partial revert of C (kept critical fixes)"
Use Case:
- You revert a feature but want to keep a bug fix from the same commit.
3. Combining Reverts with Fixes (Atomic Undo + Patch)
Problem:
You want to revert a commit but immediately apply a fix in the same commit.
Solution:
1. Revert with --no-commit
.
2. Apply fixes.
3. Commit everything together.
Example:
git revert --no-commit B
# Fix a related bug
echo "Hotfix for edge case" >> file.txt
git add .
git commit -m "Reverted B + added critical hotfix"
Result:
- Clean history with one commit for both revert and fix.
4. Reverting a Merge Commit (Advanced Conflict Handling)
Problem:
Merge commits (git merge
) are tricky to revert because they involve multiple parents.
Solution:
Use -m
to specify the mainline parent (usually 1
for the branch you merged into).
Example:
git revert --no-commit -m 1 MERGE_COMMIT_HASH
# Resolve conflicts if any
git commit -m "Reverted problematic merge"
When to Use:
- When a
git merge
introduced bugs and needs undoing.
5. Dry Run Before Reverting (Safety Check)
Problem:
You want to preview what git revert
will do before applying it.
Solution:
Use git revert -n --no-commit
+ git diff
to inspect changes.
Example:
git revert -n B # Same as --no-commit
git diff # See what changes will be undone
# If okay:
git commit -m "Reverted B after review"
# If not okay:
git reset --hard
Benefit:
- Avoids accidental reverts by checking changes first.
Conclusion
git revert --no-commit
is a powerful, flexible tool for undoing changes without immediately committing them. Whether you're a beginner learning Git basics, an intermediate user optimizing workflows, or an advanced developer handling complex reverts, this command helps you:
- Revert multiple commits in one go (cleaner history).
- Partially undo changes while keeping critical fixes.
- Resolve conflicts before finalizing (safer reverts).
- Combine reverts with new fixes in a single commit.
Quick Cheat Sheet
Command | Effect |
---|---|
git revert --no-commit B |
Reverts B but waits for commit |
git revert --no-commit B^..D |
Reverts B to D in one step |
git revert -n B && git diff |
Preview revert changes |
git revert --abort |
Cancel an ongoing revert (Git 2.23+) |
git revert -m 1 MERGE_COMMIT |
Revert a merge commit |
Up Next in the Series: git stash --patch
– Stash only specific changes interactively
Daily advance GIT tips in your inbox---worth starting? Respond to my poll here🚀
For more useful and innovative tips and tricks, Let's connect on Medium
Top comments (0)
Some comments may only be visible to logged-in visitors. Sign in to view all comments.