Git
version control
commit removal
software development
GitHub

Remove specific commit

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

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.

bash
git log --oneline
git revert abc1234

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.

bash
git rebase -i HEAD~5

Git opens a todo list such as:

text
1pick 1111111 add feature flag
2pick 2222222 refactor login flow
3pick 3333333 accidental debug commit
4pick 4444444 add tests
5pick 5555555 update docs

To remove the unwanted commit, delete its line or change pick to drop.

text
1pick 1111111 add feature flag
2pick 2222222 refactor login flow
3drop 3333333 accidental debug commit
4pick 4444444 add tests
5pick 5555555 update docs

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:

text
A - B - C - D - E

and want to remove C, you can replay everything after C onto B.

bash
git rebase --onto B C E

That effectively transforms the history into:

text
A - B - D' - E'

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.

bash
git reset --hard HEAD~1

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.

bash
git push --force-with-lease origin my-branch

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 revert when 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 --onto for precise history surgery when you understand the graph.
  • 'git reset mainly 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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.