Clean up historyAlways fixable

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.

quick fix
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

  1. Find the commit

    Confirm exactly which commit you mean and how far back it is, so the rebase range is right.

    step 1
    git log --oneline -10
    git show a1b2c3d
    
  2. Drop 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 2
    git rebase -i HEAD~5
    
    #   pick a1b2c3d Good commit
    #   drop f4e5d6c Commit to remove     <- changed from pick
    #   pick 9a8b7c6 Another good commit
    
  3. Resolve 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 3
    git add .
    git rebase --continue
    
    # or back out entirely:
    git rebase --abort
    
  4. Already 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 4
    git revert a1b2c3d
    git push
    
  5. Push a rewritten private branch

    All the hashes after the dropped commit changed, so the push needs a lease-protected force.

    step 5
    git 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

git rebase -i

Reorder, squash, split, reword or drop your recent commits.

git revert

Create a new commit that undoes an earlier one, leaving history intact.

git reflog

The undo history for everything - the single most important recovery command.

git push

Upload your commits to the remote repository.

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

← All Git Rescue guides