local-files
overwrite
pull
git

How do I force "git pull" to overwrite local files?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

There is no dedicated git pull --overwrite-my-work command, because overwriting local files is a destructive operation that Git makes you express explicitly. The safe answer is to decide first whether you want to discard local changes, save them temporarily, or replay them on top of the remote branch.

If you want to throw away local changes

When the goal is to make the local branch match the remote exactly, fetch first and then reset hard to the remote branch tip.

bash
git fetch origin
git reset --hard origin/main

Replace main with your branch name if needed.

This does two things:

  • 'git fetch updates your knowledge of the remote'
  • 'git reset --hard moves the current branch and working tree to that remote commit'

If untracked files are also in the way, you may need:

bash
git clean -fd

That removes untracked files and directories, so do not run it casually.

If you want to keep the local changes for later

Stashing is the safer choice when you may want the work back:

bash
git stash push -u -m "save local work before pull"
git pull
git stash pop

Use this when you want to update the branch but are not ready to discard local edits permanently.

If stash pop causes conflicts, resolve them as a normal merge conflict rather than trying to force another pull.

If you want the remote changes plus your local commits

If your local work is already committed, the right operation is usually rebase rather than overwrite:

bash
git pull --rebase

This replays local commits on top of the newly fetched remote commits. It does not discard your committed history, and it avoids an unnecessary merge commit in many workflows.

For a manual equivalent:

bash
git fetch origin
git rebase origin/main

This is usually a better answer than hard reset when your work matters.

Check what you are about to destroy

Before using destructive commands, inspect your current state:

bash
git status
git log --oneline --decorate --graph -n 10
git diff

These commands tell you whether you are discarding uncommitted edits, local commits, or both.

If you have local commits that are not pushed anywhere, a hard reset will remove them from the branch tip. They may still be recoverable through reflog, but recovery should not be your primary plan.

A complete force-sync pattern

If the user really wants the local checkout to become identical to the remote branch and understands the data loss risk, this is the canonical sequence:

bash
git fetch origin
git reset --hard origin/main
git clean -fd

That handles:

  • tracked file changes
  • staged changes
  • local commits on the current branch
  • untracked files

Again, it is intentionally destructive.

Recovering after a mistaken reset

If you reset too aggressively, git reflog is often the first recovery tool:

bash
git reflog

You can usually find the previous HEAD position and reset back to it:

bash
git reset --hard HEAD@{1}

This is not guaranteed for every situation, but it is often enough to rescue recent work.

Common Pitfalls

The most common mistake is using git pull when the real intent is "make my branch identical to the remote". Another is running git reset --hard without checking whether local commits exist only on the current machine. Developers also forget that reset --hard does not remove untracked files, so the working tree still looks wrong until git clean is used. Stashing is often skipped even when the user is unsure whether the local work may matter later. Finally, people sometimes ask Git to "force pull" when what they really need is pull --rebase, which preserves local commits instead of discarding them.

Summary

  • To discard local tracked changes and match the remote exactly, use git fetch plus git reset --hard.
  • Use git clean -fd if untracked files also need to be removed.
  • Use git stash if you want to save local work before pulling.
  • Use git pull --rebase when the goal is to keep local commits and replay them on top of the remote.
  • Inspect git status, git diff, and recent history before destructive commands.
  • If you reset by mistake, check git reflog immediately for recovery options.

Course illustration
Course illustration

All Rights Reserved.