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.
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:
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.
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:
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.
Example output might show:
- '
HEAD@{0}current state' - '
HEAD@{1}pull finished' - '
HEAD@{2}state before pull'
Then you can reset explicitly:
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:
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:
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 --hardwithout 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 reflogand guessing which commit to return to. - Confusing
git fetchstate with the changes actually integrated bygit pull.
Summary
- Undoing a pull starts with identifying whether the pull merged, fast-forwarded, or rebased.
- '
git reflogis the most reliable tool for finding the pre-pull state.' - '
git reset --hard ORIG_HEADis 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
- How to undo git commit --amend done instead of git commit
- How to undo local changes to a specific file
- How to unstage large number of files without deleting the content
- How to unstash only certain files?
- How to update a file in remote repo, without cloning that repo first?
- How to update a git shallow clone?
- How to update a pull request from forked repo?
- How to update git commit author, but keep original date when amending?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.