How do I force an overwrite of local files on a git pull
? My local repository contains a file of the same filename as on the server.
error: Untracked working tree file 'example.txt' would be overwritten by merge
gitgit-fetchgit-pulloverwriteversion-control
How do I force an overwrite of local files on a git pull
? My local repository contains a file of the same filename as on the server.
error: Untracked working tree file 'example.txt' would be overwritten by merge
Best Answer
First, update all
origin/<branch>
refs to latest:Backup your current branch (e.g.
master
):Jump to the latest commit on
origin/master
and checkout those files:Explanation:
git fetch
downloads the latest from remote without trying to merge or rebase anything.git reset
resets the master branch to what you just fetched. The--hard
option changes all the files in your working tree to match the files inorigin/master
.Maintain current local commits
[*]: It's worth noting that it is possible to maintain current local commits by creating a branch from
master
before resetting:After this, all of the old commits will be kept in
new-branch-to-save-current-commits
.Uncommitted changes
Uncommitted changes, even if staged (with
git add
), will be lost. Make sure tostash
or commit anything you need. For example, run the following:And later (after
git reset
), reapply these uncommitted changes: