Delete a commit from the middle of your history
How do I remove a single commit without losing the ones after it?
Short answer
Mark it drop in git rebase -i and Git replays everything except that commit. On a shared branch, git revert is the safe alternative — it neutralises the change without rewriting anyone's history.
git rebase -i HEAD~5
# change 'pick' to 'drop' on the commit you want gone
Removes one commit and replays the rest on top
Does this match your situation?
- A commit added something that should never have been committed.
- An experiment got committed in the middle of real work.
- You want a commit gone but need to keep everything after it.
Step-by-step fix
Find the commit
Confirm exactly which commit you mean and how far back it is, so the rebase range is right.
step 1git log --oneline -10 git show a1b2c3dDrop it with an interactive rebase
Change its action to drop, or simply delete the whole line — both work. Everything else replays in order without it.
step 2git rebase -i HEAD~5 # pick a1b2c3d Good commit # drop f4e5d6c Commit to remove <- changed from pick # pick 9a8b7c6 Another good commitResolve conflicts if later commits depended on it
If subsequent commits built on what you removed, their diffs may no longer apply. Fix the files and continue, or abort if the dependency turns out to be real.
step 3git add . git rebase --continue # or back out entirely: git rebase --abortAlready pushed to a shared branch? Revert instead
Revert adds a commit that undoes the change, leaving history intact so nobody else has to do anything.
step 4git revert a1b2c3d git pushPush a rewritten private branch
All the hashes after the dropped commit changed, so the push needs a lease-protected force.
step 5git push --force-with-lease
Why this works
Interactive rebase rebuilds your branch commit by commit from a starting point, so omitting one simply means never replaying it. The commits after it are recreated with new parents and therefore new hashes, which is why this is a history rewrite rather than a deletion. The dropped commit itself survives in the object database and the reflog until garbage collection, so a mistake here is recoverable for weeks.
How to stop it happening again
- Keep experiments on a scratch branch you can delete wholesale.
- Review with git diff --staged before committing.
- Prefer small commits - they're far easier to drop cleanly.
Commands used in this guide
Still stuck?
Search the full command reference and every other rescue guide — there are 56 of them, covering everything from detached HEAD to force-push disasters.
Browse all guides →Frequently asked questions
How do I delete a commit but keep the ones after it?
Run git rebase -i with a range that includes it, then mark that commit drop (or delete its line). Git replays the rest without it. Expect conflicts if later commits built on what you removed.
What's the difference between dropping and reverting a commit?
Dropping erases the commit from history and changes every hash after it, which is only appropriate on a private branch. Reverting adds a new commit that undoes the change, leaving history intact - the right choice for anything already pushed.
Can I recover a commit I dropped by mistake?
Yes. It's still in the object database and listed in git reflog. Find its hash and cherry-pick it back, or reset to the pre-rebase state with git reset --hard ORIG_HEAD.
Related rescue guides
How do I change the order of commits in Git?
Run git rebase -i and move the lines in the editor — Git replays them in whatever order you leave them. Conflicts happen when the commits touch the same code, in which case reordering may not be possible without editing.
Read the fix →Always fixableHow do I undo a commit that's already on the remote?
On a shared branch, use git revert <hash> — it adds a new commit that reverses the change, so nobody else has to do anything. Rewriting with reset plus a force-push is only appropriate on a branch you're certain nobody else has pulled.
Read the fix →Always fixableHow do I edit a commit that isn't the most recent one?
Mark the commit edit in git rebase -i. Git stops there, you make your changes and amend, then git rebase --continue replays the rest. The tidier route is git commit --fixup plus an autosquash rebase.
Read the fix →Always fixableHow do I combine multiple commits into a single commit?
git rebase -i HEAD~n opens an editor where you mark commits as squash or fixup to fold them into the one above. For a quick all-in-one collapse, git reset --soft HEAD~n followed by a fresh commit achieves the same result with no editor at all.
Read the fix →