How to force git pull to overwrite local files
You have local edits, the remote has moved on, and you want the remote’s
version — all of it, yours discarded. git pull refuses, so the obvious next
move is to add --force. That does not work, and it is worth seeing why.
What git tells you first
$ git pull
error: Your local changes to the following files would be overwritten by merge:
file.txt
Please commit your changes or stash them before you merge.
Aborting
This is git refusing to throw your work away without being told to. Fair enough.
–force does not mean what it looks like
$ git pull --force
error: Your local changes to the following files would be overwritten by merge:
file.txt
Aborting
file still : line one|my local edit|
The identical error. Nothing was overwritten.
--force on git pull is passed through
to the fetch, where it permits a non-fast-forward update of a
remote-tracking ref. It has nothing to say about your working tree. So the one
flag everybody tries is the one flag that cannot help.
What actually works
$ git fetch origin
$ git reset --hard origin/main
file now : line one|from the colleague|
status : ''
Two steps, and the split matters.
git fetch brings the remote’s state down
without touching anything of yours.
git reset --hard then moves your branch
to origin/main and overwrites the working tree to match.
Note that it is origin/main and not main. You are resetting to the
remote-tracking ref you just updated.
It does not clean everything
after reset --hard : file.txt untracked.txt
after git clean -fd : file.txt
reset --hard only deals with tracked files. Anything untracked — build
output, an editor backup, a file you added but never staged — survives it
untouched.
If you want a genuinely clean checkout,
git clean is the second half:
$ git clean -fd
-f because git refuses to delete without it, -d to include directories. Run
git clean -nd first — the -n prints what it would remove and deletes
nothing. There is no undo for this one; untracked files are not in the object
database, so there is nothing to recover from.
The version that does not throw work away
Usually “overwrite my files” really means “I want the remote’s version, and I have not thought hard about mine”. If there is any doubt, park your work instead:
$ git stash
$ git pull
$ git stash pop
Auto-merging file.txt
CONFLICT (content): Merge conflict in file.txt
status : UU file.txt
That conflict is the point. Instead of choosing between your work and the
remote before seeing either, you now have both in the file and can decide line
by line. If it turns out you did not want your version after all,
git checkout --theirs file.txt or a fresh git reset --hard origin/main is
still available — and the stash is still in git stash list either way.
About Netcup (advertisement)
The German host Netcup offers, among other things, affordable and powerful web hosting packages, KVM-based root servers and dedicated servers. With our voucher codes you can save even more (6€ off your first order, 30% off all KVM-based root servers, ...).