Remove specific commit
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Removing a specific Git commit can mean two very different things: undoing its effect while preserving history, or rewriting history so the commit disappears entirely. The right choice depends on whether the branch is already shared with other people.
Use git revert for Shared History
If the commit is already on a branch that other people may have pulled, the safest option is usually git revert. Revert creates a new commit that applies the inverse of the selected commit.
That leaves the original commit in the history, which is exactly why it is safe for public branches. Nobody else's history has to be rewritten.
Use this when:
- the branch has been pushed and shared
- you want an audit trail showing what was undone
- you want to avoid force-pushing
Use Interactive Rebase to Drop a Commit From Local History
If the branch is still private or you are intentionally rewriting history, interactive rebase gives precise control.
Suppose the bad commit is somewhere in your last five commits.
Git opens a todo list such as:
To remove the unwanted commit, delete its line or change pick to drop.
After saving, Git rewrites the branch without that commit.
Use rebase --onto When You Know the Exact Commit Range
For scriptable or more explicit history surgery, git rebase --onto can remove a middle commit.
If you have history like this:
and want to remove C, you can replay everything after C onto B.
That effectively transforms the history into:
This is powerful, but it is easier to misuse than interactive rebase, so it is usually better for people who already understand the parent-child structure of the affected commits.
git reset Is Not the General Answer
People often reach for git reset, but reset is mainly for moving the current branch tip. It is useful when the commit you want to remove is at the end of the branch, not buried in the middle.
That removes the latest commit from the branch pointer entirely. It does not selectively remove an older commit while keeping newer ones.
So if the goal is "remove one specific older commit," interactive rebase or revert is usually the right tool.
What to Do After Rewriting History
If you rewrote a branch that already exists on the remote, the next push usually requires a force update.
Use --force-with-lease, not plain --force. It protects you from overwriting remote work you have not seen.
Also communicate with teammates before doing this on any branch others might be using.
Common Pitfalls
The biggest mistake is rewriting public history when a simple revert would have solved the problem safely.
Another mistake is using git reset --hard without understanding that it can discard local work permanently.
A third issue is forgetting that removing a commit can create conflicts in later commits that depended on it. Rebase may stop and require manual resolution.
Finally, removing a commit from Git history does not necessarily erase sensitive data from every clone, cache, or remote reference. Secret exposure often requires rotation and more aggressive history-cleaning procedures.
Summary
- Use
git revertwhen the commit is already shared and you want a safe undo. - Use interactive rebase when you want the commit to disappear from local history.
- Use
rebase --ontofor precise history surgery when you understand the graph. - '
git resetmainly removes commits from the branch tip, not arbitrary middle commits.' - After rewriting remote history, push with
--force-with-lease. - Choose the method based on whether preserving or rewriting history is the safer outcome.
Related reading
- Remove tracking branches no longer on remote
- Removing multiple files from a Git repo that have already been deleted from disk
- Removing multiple files from a Git repo that have already been deleted from disk
- Rename master branch for both local and remote Git repositories
- Rename master branch for both local and remote Git repositories
- Renaming a branch in GitHub
- Renaming branches remotely in Git
- Replace remote tag with Git
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.