git
version control
git push
git pull
developer tips

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:

bash
git fetch origin

Then inspect the difference:

bash
git switch my-branch
git log --oneline --graph --decorate HEAD..origin/my-branch
git log --oneline --graph --decorate origin/my-branch..HEAD

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:

bash
git switch my-branch
git fetch origin
git reset --hard origin/my-branch

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:

bash
1git switch my-branch
2git branch backup/my-branch
3git fetch origin
4git rebase origin/my-branch

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:

bash
git switch -c repaired-branch origin/my-branch
git cherry-pick <commit-id>

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:

bash
git stash push -u -m "save work before force-push recovery"

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:

bash
git branch backup/pre-reset

If something important disappears, the backup branch or git reflog gets you back.

Mistakes to avoid

  • Using git reset --hard without 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 reflog after a bad recovery step wastes one of Git's best safety nets.

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
Basis Lab
No 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 course
Interview Questions practice on Codemia

Over 11,000 interview practice questions for 300+ companies, searchable by company and role.

Browse interview questions