How to Force git pull to Overwrite Local Files

By 

•

Published on

•

8 min read

Force a local Git branch to match the remote branch

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.

Warning
The reset command used in this guide discards uncommitted changes to tracked files and removes local-only commits from the current branch history. Back up valuable work before continuing.

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:

Terminal
git status --short --branch

The 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:

Terminal
git branch --show-current

The 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:

Terminal
git remote -v

The 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:

Terminal
git fetch origin
git log --oneline origin/main..HEAD

An 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:

Terminal
git branch backup-before-reset

This 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:

Terminal
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:

Terminal
git fetch origin

After the fetch, origin/main represents the current main branch on the remote. Reset the checked-out local branch to that commit:

Terminal
git reset --hard origin/main

The --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:

Terminal
git status --short --branch

The 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:

Terminal
git rev-parse HEAD origin/main

Both 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:

Terminal
git clean -nd

The -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:

Terminal
git clean -fd

Ignored 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:

Terminal
git switch develop

Fetch from upstream, then reset to its develop branch:

Terminal
git fetch upstream
git reset --hard upstream/develop

If the local branch does not exist yet, create it directly from the remote-tracking branch instead:

Terminal
git switch -c develop --track upstream/develop

See 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:

Terminal
git switch backup-before-reset

You 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:

Terminal
git stash pop

Applying 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:

Terminal
git reflog

Immediately 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:

Terminal
git branch recovered-work ORIG_HEAD

Reflog 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:

Terminal
git fetch origin
git rebase origin/main

For uncommitted changes, stash them, update the branch, and then apply the stash:

Terminal
git stash push -u
git pull --ff-only
git stash pop

The --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 merge
git 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.

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

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