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
git reset --hard origin/branch_to_overwrite
- anyone git branch <branch> -D
2. Reset to a commit before the conflict: git reset <commit> --hard
3. Re-create the branch: git branch <branch>
4. Set tracking to the server: git --set-upstream-to=origin/<branch> <branch> 5. Pull:
git pull` - anyone git config core.autocrlf false; git ls-files -z | xargs -0 rm; git checkout .
- anyone ⚠ Warning:
Any uncommitted local change to tracked files will be lost, even if staged.
But any local file that's not tracked by Git will not be affected.
First, update all origin/<branch>
refs to latest:
git fetch --all
Backup your current branch (e.g. master
):
git branch backup-master
Jump to the latest commit on origin/master
and checkout those files:
git reset --hard origin/master
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 in origin/master
.
[*]: It's worth noting that it is possible to maintain current local commits by creating a branch from master
before resetting:
git checkout master
git branch new-branch-to-save-current-commits
git fetch --all
git reset --hard origin/master
After this, all of the old commits will be kept in new-branch-to-save-current-commits
.
Uncommitted changes, even if staged (with git add
), will be lost. Make sure to stash
or commit anything you need. For example, run the following:
git stash
And later (after git reset
), reapply these uncommitted changes:
git stash pop
Which may create merge conflicts.
Answered 2023-09-20 19:53:22
git reset --hard origin/branch-name
- anyone git pull -f
- anyone git reflog
, which list all commits, also those without a base. Until you cleanup your local copy using git gc
, then all is lost - anyone This will remove all uncommitted changes, even if staged,
and then pull:
git reset --hard HEAD
git pull
But any local file that's not tracked by Git will not be affected.
Answered 2023-09-20 19:53:22
WARNING: git clean
deletes all your untracked files/directories and can't be undone.
Sometimes just clean -f
does not help. In case you have untracked DIRECTORIES, -d option also needed:
# WARNING: this can't be undone!
git reset --hard HEAD
git clean -f -d
git pull
WARNING: git clean
deletes all your untracked files/directories and can't be undone.
Consider using -n
(--dry-run
) flag first. This will show you what will be deleted without actually deleting anything:
git clean -n -f -d
Example output:
Would remove untracked-file-1.txt
Would remove untracked-file-2.txt
Would remove untracked/folder
...
Answered 2023-09-20 19:53:22
.gitignore
- anyone git clean -dfx
. The -x
ignores .gitignore. Typically your build products will be in .gitignore. - anyone Like Hedgehog I think the answers are terrible. But though Hedgehog's answer might be better, I don't think it is as elegant as it could be. The way I found to do this is by using fetch
and merge
with a defined strategy. Which should make it so that your local changes are preserved as long as they are not one of the files that you are trying to force an overwrite with.
git add *
git commit -a -m "local file server commit message"
git fetch origin master
git merge -s recursive -X theirs origin/master
-X
is an option name, and theirs
is the value for that option. You're choosing to use their
changes (the other option is ours
changes) if there is a conflict.
Answered 2023-09-20 19:53:22
get fetch other-repo
; 2) git merge -s recursive -X theirs other-repo/master
- anyone git merge -X theirs origin/master
- anyone Instead of doing:
git fetch --all
git reset --hard origin/master
I'd advise doing the following:
git fetch origin master
git reset --hard origin/master
No need to fetch all remotes and branches if you're going to reset to the origin/master branch right?
Answered 2023-09-20 19:53:22
It looks like the best way is to first do:
git clean
To delete all untracked files and then continue with the usual git pull
...
Answered 2023-09-20 19:53:22
git fetch origin && git reset --hard origin/master
- anyone git clean
the best answer here? Seems like removing files isn't necessarily what the OP wants. They asked for 'an overwrite of local files' not deletion. - anyone 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}
Answered 2023-09-20 19:53:22
--include-untracked
simply by temporarily git add
-ing your entire repo, then immediately stashing it. - anyone git stash apply
brought back all my untracked files with the exception (rightly) of the ones that the merge had already created: "already exists, no checkout." Worked perfectly. - anyone git stash -u
. - anyone You might find this command helpful to throw away local changes:
git checkout <your-branch> -f
And then do a cleanup (removes untracked files from the working tree):
git clean -f
If you want to remove untracked directories in addition to untracked files:
git clean -fd
Answered 2023-09-20 19:53:22
Instead of merging with git pull
, try this:
git fetch --all
followed by:
git reset --hard origin/master
.
Answered 2023-09-20 19:53:22
The only thing that worked for me was:
git reset --hard HEAD~5
This will take you back five commits and then with
git pull
I found that by looking up how to undo a Git merge.
Answered 2023-09-20 19:53:22
work around
but really effective. Because some conflicts may happen just in few commits then reverting 5 commits will make sure no conflicts with remote code. - anyone 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 webserver, 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 the newest data.
The second command checks if there are any files that 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.
Answered 2023-09-20 19:53:22
git merge origin/master
will be faster and probably even safer. Since if someone pushed new changes during the removal of of files of this script (which is not likely to happen, but possible), the whole pull could fail. The only reason I put pull
in there is because someone might not be working on the master branch, but some other branch and I wanted the script to be universal. - anyone .gitignore
. - anyone First of all, try the standard way:
git reset HEAD --hard # To remove all not committed changes!
git clean -fd # To remove all untracked (non-git) files and folders!
Warning: Above commands can results in data/files loss only if you don't have them committed! If you're not sure, make the backup first of your whole repository folder.
Then pull it again.
If above won't help and you don't care about your untracked files/directories (make the backup first just in case), try the following simple steps:
cd your_git_repo # where 'your_git_repo' is your git repository folder
rm -rfv * # WARNING: only run inside your git repository!
git pull # pull the sources again
This will REMOVE all git files (excempt .git/
dir, where you have all commits) and pull it again.
Why git reset HEAD --hard
could fail in some cases?
Custom rules in .gitattributes file
Having eol=lf
rule in .gitattributes could cause git to modify some file changes by converting CRLF line-endings into LF in some text files.
If that's the case, you've to commit these CRLF/LF changes (by reviewing them in git status
), or try: git config core.autcrlf false
to temporary ignore them.
File system incompability
When you're using file-system which doesn't support permission attributes.
In example you have two repositories, one on Linux/Mac (ext3
/hfs+
) and another one on FAT32/NTFS based file-system.
As you notice, there are two different kind of file systems, so the one which doesn't support Unix permissions basically can't reset file permissions on system which doesn't support that kind of permissions, so no matter how --hard
you try, git always detect some "changes".
Answered 2023-09-20 19:53:22
In speaking of pull/fetch/merge in the previous answers, I would like to share an interesting and productive trick,
git pull --rebase
This above command is the most useful command in my Git life which saved a lot of time.
Before pushing your newly commit to server, try this command and it will automatically synchronise the latest server changes (with a fetch + merge) and will place your commit at the top in the Git log. There isn't any need to worry about manual pull/merge.
Find details in What does "git pull --rebase" do?.
Answered 2023-09-20 19:53:22
git pull -r
. - anyone git add -A
, 2) git commit -m
3) and finally git pull rebase
. Thank you. - anyone I had the same problem. No one gave me this solution, but it worked for me.
I solved it by:
.git
directory.git reset --hard HEAD
git pull
git push
Now it works.
Answered 2023-09-20 19:53:22
Here is a generic solution if you do not always want to paste the branch name or you want to automate this within a script
git fetch
git reset --keep origin/$(git rev-parse --abbrev-ref HEAD)
If you want to reset your local changes too:
git fetch
git reset --hard origin/$(git rev-parse --abbrev-ref HEAD)
You also could add a bash alias using this command:
alias gplf='git fetch && echo "HEAD was at $(git rev-parse --short HEAD)" && git reset --hard origin/$(git rev-parse --abbrev-ref HEAD)'
Answered 2023-09-20 19:53:22
alias gplf='git fetch && echo "HEAD was at $(git rev-parse --short HEAD)" && git reset --hard origin/$(git rev-parse --abbrev-ref HEAD)'
- anyone git fetch
followed by git reset --hard origin/$(git rev-parse --abbrev-ref HEAD)
. I chose it for its simplicity and the recentness of the answer. - anyone I had a similar problem. I had to do this:
git reset --hard HEAD
git clean -f
git pull
Answered 2023-09-20 19:53:22
git clean
with caution - anyone I summarized other answers. You can execute git pull
without errors:
git fetch --all
git reset --hard origin/master
git reset --hard HEAD
git clean -f -d
git pull
Warning: This script is very powerful, so you could lose your changes.
Answered 2023-09-20 19:53:22
git reset --hard HEAD
may be redundant; my local man page (2.6.3) say that reset
in the second line git reset --hard origin/master
"defaults to HEAD in all forms." - anyone Based on my own similar experiences, the solution offered by Strahinja Kustudic above is by far the best. As others have pointed out, simply doing hard reset will remove all the untracked files which could include lots of things that you don't want removed, such as config files. What is safer, is to remove only the files that are about to be added, and for that matter, you'd likely also want to checkout any locally-modified files that are about to be updated.
That in mind, I updated Kustudic's script to do just that. I also fixed a typo (a missing ' in the original).
#/bin/sh
# 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
echo "Deleting untracked file $file..."
rm -vf "$file"
done
# Checkout all files which have been locally modified
for file in `git diff HEAD..origin/master --name-status | awk '/^M/ {print $2}'`
do
echo "Checking out modified file $file..."
git checkout $file
done
# Finally merge all the changes (you could use merge here as well)
git pull
Answered 2023-09-20 19:53:22
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!
Answered 2023-09-20 19:53:22
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
.
Answered 2023-09-20 19:53:22
I believe there are two possible causes of conflict, which must be solved separately, and as far as I can tell none of the above answers deals with both:
Local files that are untracked need to be deleted, either manually (safer) or as suggested in other answers, by git clean -f -d
Local commits that are not on the remote branch need to be deleted as well. IMO the easiest way to achieve this is with: git reset --hard origin/master
(replace 'master' by whatever branch you are working on, and run a git fetch origin
first)
Answered 2023-09-20 19:53:22
I am not sure why anyone did not talk about FETCH_HEAD
yet.
git fetch origin master && git reset --hard FETCH_HEAD
If you want to put it in an alias, the command would be:
git config --global alias.fpull '!git fetch origin master && git reset --hard FETCH_HEAD'
Answered 2023-09-20 19:53:22
&
with &&
. - anyone 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
Answered 2023-09-20 19:53:22
I have a strange situation that neither git clean
or git reset
works. I have to remove the conflicting file from git index
by using the following script on every untracked file:
git rm [file]
Then I am able to pull just fine.
Answered 2023-09-20 19:53:22
git update-index --assume-unchanged <file>
- anyone I know of a much easier and less painful method:
$ git branch -m [branch_to_force_pull] tmp
$ git fetch
$ git checkout [branch_to_force_pull]
$ git branch -D tmp
That's it!
Answered 2023-09-20 19:53:22
origin/<branch_to_force_pull>
. - anyone I just solved this myself by:
git checkout -b tmp # "tmp" or pick a better name for your local changes branch
git add -A
git commit -m 'tmp'
git pull
git checkout master # Or whatever branch you were on originally
git pull
git diff tmp
where the last command gives a list of what your local changes were. Keep modifying the "tmp" branch until it is acceptable and then merge back onto master with:
git checkout master && git merge tmp
For next time, you can probably handle this in a cleaner way by looking up "git stash branch" though stash is likely to cause you trouble on the first few tries, so do first experiment on a non-critical project...
Answered 2023-09-20 19:53:22
Just do
git fetch origin branchname
git checkout -f origin/branchname // This will overwrite ONLY new included files
git checkout branchname
git merge origin/branchname
So you avoid all unwanted side effects, like deleting files or directories you wanted to keep, etc.
Answered 2023-09-20 19:53:22
checkout -f
into the branch I wanted to merge from, that got rid of all the problematic untracked files. Then I could checkout again my destination, and finally merge without issues. - anyone Requirements:
Solution:
Fetch with a clean of files and directories ignoring .gitignore and hard reset to origin.
git stash --include-untracked
git fetch --all
git clean -fdx
git reset --hard origin/master
Answered 2023-09-20 19:53:22
Reset the index and the head to origin/master
, but do not reset the working tree:
git reset origin/master
Answered 2023-09-20 19:53:22
These four commands work for me.
git reset --hard HEAD
git checkout origin/master
git branch -D master
git checkout -b master
To check/pull after executing these commands
git pull origin master
I tried a lot but finally got success with these commands.
Answered 2023-09-20 19:53:22