How to Force git pull to Overwrite Local Files

When a local Git branch has diverged from its remote, you may want to discard the local version and start again from the remote copy. A normal git pull will not do that. It fetches remote changes and then integrates them according to your options and configuration. It may stop because the branches have diverged or because your local changes would be overwritten.
This guide explains how to back up anything you may need, fetch the current remote state, and reset your local branch so its tracked files and commit history match the remote branch.
Why git pull –force Does Not Overwrite Local Files
The git pull command is a combination of two operations: it runs git fetch and then integrates the fetched branch with your current branch. Depending on your options and configuration, Git may fast-forward, merge, rebase, or refuse to update a diverged branch. You can read more about that distinction in our git fetch vs git pull guide
.
Adding --force does not turn git pull into a command that discards your working tree. The flag applies to the fetch operation and allows certain reference updates, but Git still tries to integrate the histories afterward. To replace the local branch, use git fetch followed by git reset --hard.
Check What Will Be Replaced
Start by checking the current branch and working tree:
git status --short --branchThe first line shows the checked-out branch and whether it is ahead of or behind its upstream. The remaining lines list staged, modified, and untracked files. If the command prints only a branch line, the working tree is clean.
Confirm the branch name separately before constructing the reset command:
git branch --show-currentThe examples below use main and a remote named origin. Replace both names if your repository uses something different. List the configured remotes when you are unsure:
git remote -vThe ahead and behind counts come from the last fetch, so they can be out of date. To see exactly which local commits the reset will remove, fetch the remote and list the commits that exist only on your branch:
git fetch origin
git log --oneline origin/main..HEADAn empty result means there are no local-only commits to lose.
Back Up Local Commits and Files
A backup branch preserves commits that exist only on the current branch. Create one before the reset:
git branch backup-before-resetThis new branch points to the current commit, but it does not save uncommitted file changes. Use git stash
with -u to save staged, unstaged, and untracked files:
git stash push -u -m "Before replacing local branch"The backup branch and stash protect different work. The branch keeps local commits reachable, while the stash stores changes that have not been committed. You can inspect the saved stash with git stash list before proceeding.
The -u option does not include ignored files such as .env. Copy any valuable ignored files outside the repository before resetting. If Git reports No local changes to save, it has not created a new stash entry; do not restore an older stash by mistake.
Force the Local Branch to Match the Remote
Fetch the latest objects and update the remote-tracking branches without changing your working tree:
git fetch originAfter the fetch, origin/main represents the current main branch on the remote. Reset the checked-out local branch to that commit:
git reset --hard origin/mainThe --hard mode moves the current branch, updates the staging area, and replaces tracked working-tree files with the versions from origin/main. Tracked files that do not exist in the remote commit are removed. Unrelated untracked files normally remain, although an untracked file can be overwritten when it blocks a tracked path that Git needs to create.
Check the result:
git status --short --branchThe branch should now be even with origin/main, and there should be no modified tracked files. You can also compare the two commit IDs directly:
git rev-parse HEAD origin/mainBoth lines should contain the same commit hash.
Remove Remaining Untracked Files
git reset --hard is not a general cleanup command for untracked files and directories. Preview what Git would remove with git clean
before deleting anything:
git clean -ndThe -n option performs a dry run, and -d includes untracked directories. Review every path in the output. If all listed files can be deleted, run the cleanup with -f:
git clean -fdIgnored files, such as .env, dependency directories, and build output, are left alone by this command. The -x option would include them, but using it can delete local credentials or large generated directories. Run git clean -ndx first if you specifically need to inspect ignored files, and do not use -fdx until you have verified the entire list.
Match a Different Branch or Remote
The same workflow works with any remote-tracking branch. To replace a local develop branch with upstream/develop, first switch
to the local branch:
git switch developFetch from upstream, then reset to its develop branch:
git fetch upstream
git reset --hard upstream/developIf the local branch does not exist yet, create it directly from the remote-tracking branch instead:
git switch -c develop --track upstream/developSee the git branch guide for more ways to create and track branches.
Restore the Backup
If you need the previous local commits, switch to the backup branch you created earlier:
git switch backup-before-resetYou can keep working there, copy individual commits with git cherry-pick
, or compare it with the updated branch. To restore the stashed file changes, switch back to the updated branch with git switch main and apply the most recent stash:
git stash popApplying the stash can create conflicts when the same lines changed on the remote. Use git stash apply instead of pop when you want Git to keep the stash entry until you confirm that the restored files are correct.
If you skipped the backup branch, git reflog may still show the old commit so you can create a branch from it:
git reflogImmediately after the reset, Git also stores the previous position in ORIG_HEAD. If you have not run another reset, merge, or rebase since, create a branch from it:
git branch recovered-work ORIG_HEADReflog can recover committed work after a reset, but it cannot reconstruct uncommitted tracked changes that were discarded by git reset --hard.
Keep Local Work Instead of Overwriting It
Replacing the branch is appropriate when the local state is disposable. When you want to keep local commits, fetch first and rebase them onto the updated remote branch:
git fetch origin
git rebase origin/mainFor uncommitted changes, stash them, update the branch, and then apply the stash:
git stash push -u
git pull --ff-only
git stash popThe --ff-only option refuses to create a merge commit. If the local branch has diverged, Git stops and lets you choose whether to rebase, merge, or reset.
Troubleshooting
fatal: ambiguous argument ‘origin/main’
The remote branch does not exist locally under that name. Run git fetch origin, then use git branch -r to list remote-tracking branches. The repository may use master or another default branch instead of main.
The local branch is still ahead after the reset
Confirm that you reset the checked-out branch and used the correct remote-tracking name. Compare git branch --show-current with the target shown by git branch -vv.
Untracked files remain in the directory
This is expected because git reset --hard primarily replaces tracked content. Preview untracked cleanup with git clean -nd, then use git clean -fd only after reviewing the list.
git branch –show-current prints nothing
The repository is in a detached HEAD state. Switch to the branch you intend to replace, for example git switch main, before running the reset.
error: Your local changes to the following files would be overwritten by mergegit pull stops because the listed tracked files have uncommitted changes. Stash them with git stash push -u to keep them, or follow the reset steps above to discard them.
error: The following untracked working tree files would be overwritten by merge
The remote branch adds files at the same paths as untracked files in your directory. Move them elsewhere, or back them up and let git reset --hard origin/main replace them.
fatal: Need to specify how to reconcile divergent branches.
Both branches have commits the other does not, and Git will not pick a strategy for you. Pass --rebase or --no-rebase to keep local commits, or reset as shown in this guide to discard them.
Conclusion
Keep the backup branch and any remaining stash until you have checked the updated files and run your project tests.
Tags
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

Dejan Panovski
Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 1000+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page