Git
Git Pull
Version Control
Undo Git Commands
Git Tutorial

How to undo a git pull?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Undoing a git pull depends on what the pull actually did. A pull can fast-forward your branch, create a merge commit, or rebase local commits on top of fetched changes. The safest way to undo it is to inspect the recent history first, then reset or revert with a clear understanding of whether you are rewriting local history or producing a new commit.

Understand What git pull Changed

A plain git pull is usually git fetch plus either a merge or a fast-forward update. Some repositories configure pull to rebase instead.

Before undoing anything, inspect the current state:

bash
git status
git log --oneline --decorate -n 5
git reflog -n 10

git reflog is the most important tool here because it shows where HEAD pointed before the pull. That gives you a reliable reference for returning to the previous state.

Undo a Pull Locally with ORIG_HEAD

Git often records the previous position in ORIG_HEAD, which is convenient immediately after a pull.

bash
git reset --hard ORIG_HEAD

This moves the branch and working tree back to where they were before the pull. It is effective, but it is also destructive for uncommitted changes, so use it only when you are sure you want to discard the pulled state locally.

If you want a slightly safer pattern, inspect first:

bash
git show ORIG_HEAD --stat

That confirms where Git plans to return.

Use reflog When ORIG_HEAD Is Not Enough

If more commands were run after the pull, ORIG_HEAD may no longer be the exact target you want. reflog is the fallback.

bash
git reflog

Example output might show:

  • 'HEAD@{0} current state'
  • 'HEAD@{1} pull finished'
  • 'HEAD@{2} state before pull'

Then you can reset explicitly:

bash
git reset --hard HEAD@{2}

This is often the most accurate way to undo a recent pull in a local-only situation.

Do Something Different If the Pull Was Already Pushed

If you already pushed the pulled result to a shared remote, rewriting branch history may disrupt teammates. In that case, a new commit that reverses the unwanted changes is usually safer than reset --hard.

For example, if the pull created a merge commit:

bash
git log --oneline --decorate
git revert -m 1 <merge_commit_sha>

That preserves shared history while backing out the merge effect. The exact command depends on the type of commit the pull produced, which is why inspecting history first matters.

Protect Uncommitted Work Before Undoing

If your working tree has local edits, store them before any hard reset:

bash
git stash push -m "before undo pull"

Then you can undo the pull and decide later whether the stashed work still belongs on the branch.

The overall decision is:

  • local, unpublished pull: reset is usually fine
  • published history: prefer revert
  • uncommitted work present: stash or commit first

Common Pitfalls

  • Running git reset --hard without checking whether uncommitted work is present.
  • Undoing a pull on a shared branch by rewriting history that other people already fetched.
  • Assuming every pull created a merge commit when it may have been a fast-forward or rebase.
  • Skipping git reflog and guessing which commit to return to.
  • Confusing git fetch state with the changes actually integrated by git pull.

Summary

  • Undoing a pull starts with identifying whether the pull merged, fast-forwarded, or rebased.
  • 'git reflog is the most reliable tool for finding the pre-pull state.'
  • 'git reset --hard ORIG_HEAD is effective for local unpublished pulls but rewrites history and discards uncommitted work.'
  • If the pulled history was already pushed, reverting is usually safer than resetting.
  • Inspect first, then choose the undo method that matches your collaboration context.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.