shellstreak

Undo a merge you haven't pushed

Careful: rewrites history

Short answer

Right after the merge, run git reset --keep HEAD~1: your branch goes back to where it was before the merge.

Only if the merge isn't pushed. Pushed? See Undo a pushed merge. Still in the middle of a merge with conflicts? Use git merge --abort instead.

The commands, one by one

  1. 1git reset --keep HEAD~1

    HEAD~1 of a merge commit is the commit your branch was on before you merged. The merged branch itself isn't touched, so you can merge it again later.

  2. 2git log --oneline

    The merge commit and the branch's commits are gone from main.

Try it safely

You merged soup into main, but the soup isn't ready.

learner@shellstreak: ~
Starting your practice shell…

Type your own commands too. Reset puts the repository back to just after the mistake.

Practise this in 5 minutes

Lesson 61, reset --soft, --mixed, --hard, walks you through this undo and the ones around it in a safe terminal, step by step, and checks your answers.

Keep the terminal cheat sheet handy: free to print

Get the shellstreak newsletter

Short terminal and Git tips you can try in a minute, plus a heads-up when new free tools and lessons ship. It lands now and then, never a flood.

No spam, unsubscribe in one click.