Git: undo a pull

Fresh commits may be transmitted by cables like this one.

Okay, so you just ran git pull on a branch, and something broke, so you want to undo it. Here are two ways how.

First, see if you can still read the git pull output in your terminal’s scrollback:

$ git pull
From example.com:repo
   3ae1875..de62e8e  main           -> origin/main
Updating 3ae1875..de62e8e
Fast-forward
 cheese.md   | 2 +-
 crackers.md | 7 +++----
 2 files changed, 4 insertions(+), 5 deletions(-)

The output here tells you everything that changed during the pull. The updated range is on the “Updating” line:

Updating 3ae1875..de62e8e

This line tells you how the local branch was updated, in this case from commit 3ae1875 to commit de62e8e. You can undo the pull by moving your local branch back to the old commit with git reset:

$ git reset --keep 3ae1875

(Prefer --keep instead of --hard, as covered previously.)

But, say you’ve lost the git pull output, perhaps because you closed the terminal. In this case, you can use the reflog to find the old commit reference. The reflog is the “reference log”, a record of every branch-updating event. Run git reflog to see the entries for the current branch:

$ git reflog
de62e8e (HEAD -> main, origin/main) HEAD@{0}: pull origin main: Fast-forward
3ae1875 HEAD@{1}: commit: Bake crackers
00684df HEAD@{2}: commit: Melt stilton
...

The first entry records the pull with the commit SHA to which the branch was updated. The second line covers the previous event, which, in this case, is a commit titled “Bake crackers”. To undo the pull, it’s that second SHA you want to reset to. Again, use git reset:

$ git reset --keep 3ae1875

And there you go, you’ve undone the pull, back to how things were. When you’re ready to try the pull again, you can rerun git pull.

Fin

May your pulls be fast-forward and your merges conflict-free!

—Adam


😸😸😸 Check out my new book on using GitHub effectively, Boost Your GitHub DX! 😸😸😸


Subscribe via RSS, Twitter, Mastodon, or email:

One summary email a week, no spam, I pinky promise.

Related posts:

Tags: