Undo a changeAlways fixable

Undo a git pull that brought in changes you didn't want

How do I undo a git pull?

Short answer

A pull is a fetch plus a merge or rebase, so undoing it means moving your branch back to where it was. git reset --hard ORIG_HEAD does exactly that, because Git saves your pre-pull position there automatically.

quick fix
git reset --hard ORIG_HEAD

Returns your branch to exactly where it was before the pull

Does this match your situation?

  • A pull brought in commits that broke everything.
  • You pulled into the wrong branch.
  • A merge commit appeared that you didn't want.
  • You pulled when you meant to fetch.

Step-by-step fix

  1. Check what the pull actually did

    Look at the last few commits before undoing anything, so you know whether the pull produced a merge commit or simply fast-forwarded your branch.

    step 1
    git log --oneline -5
    git reflog -5
    
  2. Reset to ORIG_HEAD

    Git writes your previous branch tip into ORIG_HEAD before any pull, merge, rebase or reset. That makes undoing the pull you just ran a single command.

    step 2
    git reset --hard ORIG_HEAD
    

    ORIG_HEAD is overwritten by the next such operation, so use it right away or take the hash from the reflog instead.

  3. Or use the reflog if ORIG_HEAD has moved on

    The reflog lists every position your branch has held. Find the entry from just before the pull and reset to that hash.

    step 3
    git reflog
    # a1b2c3d HEAD@{0}: pull: Fast-forward
    # f4e5d6c HEAD@{1}: commit: My last commit   <- this one
    git reset --hard f4e5d6c
    
  4. If you had uncommitted changes, recover them too

    A hard reset discards uncommitted work. If the pull auto-stashed your changes they're still on the stash stack, so check before assuming they're gone.

    step 4
    git stash list
    git stash pop
    
  5. Pull properly the next time

    Fetch first and look at what's incoming; pull only once you're happy. This turns pulling from a leap into a decision.

    step 5
    git fetch origin
    git log --oneline HEAD..origin/main
    git diff HEAD...origin/main --stat
    # happy? then:
    git pull --rebase
    

Why this works

Pull isn't a special operation - it's a fetch followed by a merge or rebase, both of which just move your branch pointer and update your files. Undoing it therefore means moving the pointer back, which is exactly what reset does. ORIG_HEAD exists precisely for this: Git records where you were before any operation that significantly moves the branch, so there is always a one-command escape hatch. The fetched commits stay in your object database, harmlessly, until garbage collection.

How to stop it happening again

  • Use git fetch and inspect before you integrate, rather than pulling blind.
  • Set pull.ff only so a pull that would create a merge stops and asks.
  • Commit or stash before pulling so a reset can never cost you uncommitted work.

Commands used in this guide

git reset

Move the current branch pointer, optionally rewriting the index and your files.

git reflog

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

git pull

Fetch from the remote and immediately integrate the changes into your branch.

git fetch

Download new commits from the remote without changing any of your files.

git stash

Park uncommitted work temporarily so you can do something else.

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 undo a git pull?

git reset --hard ORIG_HEAD returns your branch to the commit it was on before the pull. If ORIG_HEAD has since been overwritten by another operation, find the pre-pull commit in git reflog and reset to that hash instead.

Does undoing a pull delete the commits I downloaded?

No. They remain in your object database and in origin/main, so they're still there and still fetchable. Only your branch pointer moves back, so your branch no longer includes them.

What is ORIG_HEAD?

A reference Git sets automatically to your previous position before a pull, merge, rebase or reset. It's a built-in one-step undo for the operation you just ran, but it's overwritten each time, so it only helps for the most recent one.

Related rescue guides

← All Git Rescue guides