How do I force an overwrite of local files on a git pull
?
The scenario is the following:
It seems like most answers here are focused on the master
branch; however, there are times when I'm working on the same feature branch in two different places and I want a rebase in one to be reflected in the other without a lot of jumping through hoops.
Based on a combination of RNA's answer and torek's answer to a similar question, I've come up with this which works splendidly:
git fetch
git reset --hard @{u}
Run this from a branch and it'll only reset your local branch to the upstream version.
This can be nicely put into a git alias (git forcepull
) as well:
git config alias.forcepull "!git fetch ; git reset --hard @{u}"
Or, in your .gitconfig
file:
[alias]
forcepull = "!git fetch ; git reset --hard @{u}"
Enjoy!
Instead of merging with git pull
, try this:
git fetch --all
followed by:
git reset --hard origin/master
.
An easier way would be to:
git checkout --theirs /path/to/file.extension
git pull origin master
This will override your local file with the file on git
The problem with all these solutions is that they are all either too complex, or, an even bigger problem, is that they remove all untracked files from the web server, which we don't want since there are always needed configuration files which are on the server and not in the Git repository.
Here is the cleanest solution which we are using:
# Fetch the newest code
git fetch
# Delete all files which are being added, so there
# are no conflicts with untracked files
for file in `git diff HEAD..origin/master --name-status | awk '/^A/ {print $2}'`
do
rm -f -- "$file"
done
# Checkout all files which were locally modified
for file in `git diff --name-status | awk '/^[CDMRTUX]/ {print $2}'`
do
git checkout -- "$file"
done
# Finally pull all the changes
# (you could merge as well e.g. 'merge origin/master')
git pull
The first command fetches newest data.
The second command checks if there are any files which are being added to the repository and deletes those untracked files from the local repository which would cause conflicts.
The third command checks-out all the files which were locally modified.
Finally we do a pull to update to the newest version, but this time without any conflicts, since untracked files which are in the repo don't exist anymore and all the locally modified files are already the same as in the repository.
Warning, doing this will permanently delete your files if you have any directory/* entries in your gitignore file.
Some answers seem to be terrible. Terrible in the sense of what happened to @Lauri by following David Avsajanishvili suggestion.
Rather (git > v1.7.6):
git stash --include-untracked
git pull
Later you can clean the stash history.
Manually, one-by-one:
$ git stash list
stash@{0}: WIP on <branch>: ...
stash@{1}: WIP on <branch>: ...
$ git stash drop stash@{0}
$ git stash drop stash@{1}
Brutally, all-at-once:
$ git stash clear
Of course if you want to go back to what you stashed:
$ git stash list
...
$ git stash apply stash@{5}
I had the same problem and for some reason, even a git clean -f -d
would not do it. Here is why: For some reason, if your file is ignored by Git (via a .gitignore entry, I assume), it still bothers about overwriting this with a later pull, but a clean will not remove it, unless you add -x
.