How to git pull after a force push
Codemia Knowledge Hub ·
After someone force-pushes a branch, don't start with git pull: the remote history has been rewritten. Fetch first, inspect how your local branch differs from the new remote branch, then decide whether to discard your local history (git reset --hard) or replay it (git rebase).
What a force push changes
A force push replaces the remote branch tip with a different commit chain. Your local branch may still point to commits that no longer exist on the remote, which is why a blind pull can produce confusing merges or refusal messages.
Update your remote-tracking refs without touching your current branch:
Then inspect the difference:
The two comparisons show what arrived from the remote and what exists only locally.
Case 1: Match the remote exactly
If your local commits are disposable or backed up elsewhere, reset the branch to the rewritten remote state:
Your local branch now matches the new remote history. This is the cleanest path when you were only tracking the branch and had no local work to keep.
Case 2: Keep your local commits
If local commits still matter, don't reset yet. Make a backup branch, then replay your work onto the new remote history:
If the rebase gets messy because the remote changed heavily, start a fresh branch from the remote and cherry-pick only the commits you want:
That is often easier when you have only a few local commits.
Stash uncommitted changes first
Don't reset or rebase over a dirty working tree. Commit or stash pending work first:
That keeps the recovery about branch history, not unrelated working-tree edits.
Why git pull hides the real decision
git pull combines fetch with merge or rebase. After a force push, the real question is whether you want the new remote history, your local commits, or both. Running git fetch separately makes that choice explicit and reduces the chance of an accidental merge commit on top of rewritten history.
Keep a recovery path
Even if you think your local commits don't matter, a backup branch is cheap insurance:
If something important disappears, the backup branch or git reflog gets you back.
Mistakes to avoid
- Using
git reset --hardwithout backing up local commits can permanently remove work you still needed. - Starting a rebase with uncommitted changes makes recovery harder than it needs to be.
- A force push did not simply add commits. It replaced part of the branch history.
- Ignoring
git reflogafter a bad recovery step wastes one of Git's best safety nets.
Related reading
- How to pull from a local branch into another one?
- How to pull remote branch from somebody else's repo
- How to push a docker image to a private repository
- How to push different local Git branches to Heroku/master
- How to re-commit a past commit if someone overwrote my commit
- How to read the output from git diff?
- How to rebase local branch onto remote master
- How to recover stashed uncommitted changes
.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 courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseInterview Questions practice on Codemia
Over 11,000 interview practice questions for 300+ companies, searchable by company and role.