可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
On my branch I had some files in .gitignore
On a different branch those files are not.
I want to merge the different branch into mine, and I don't care if those files are no longer ignored or not.
Unfortunately I get this:
The following untracked working tree files would be overwritten by merge
How would I modify my pull command to overwrite those files, without me having to find, move or delete those files myself?
回答1:
The problem is that you are not tracking the files locally but identical files are tracked remotely so in order to "pull" your system would be forced to overwrite the local files which are not version controlled.
Try running
git add *
git stash
git pull
This will track all files, remove all of your local changes to those files, and then get the files from the server.
回答2:
You can try command to clear the untracked files from the local
git 2.11 and newer
git clean -d -f .
older git
git clean -d -f ""
where -d can be replace with following:
-x means ignored files are also removed as well as files unknown to git.
-d means remove untracked directories in addition to untracked files.
-f is required to force it to run.
here is the link that can be helpful as well.
回答3:
The only commands that worked for me were:
git fetch --all
git reset --hard origin/{{your branch name}}
回答4:
If this is a one-time operation, you could just remove all untracked files from the working directory before doing the pull. Read How to remove local (untracked) files from the current Git working tree? for information on how to remove all untracked files.
Be sure to not accidentally remove untracked file that you still need ;)
回答5:
You can try that command
git clean -df
回答6:
Remove all untracked files:
git clean -d -fx .
回答7:
If you consider using the -f
flag you might first run it as a dry-run. Just that you know upfront what kind of interesting situation you will end up next ;-P
-n
--dry-run
Don’t actually remove anything, just show what would be done.
回答8:
In addition to the accepted answer you can of course remove the files if they are no longer needed by specifying the file:
git clean -f '/path/to/file/'
Remember to run it with the -n flag first if you would like to see which files git clean will remove. Note that these files will be deleted. In my case I didn't care about them anyway, so that was a better solution for me.
回答9:
How this answer differ from other answers?
The method presented here removes only files that would be overwritten by merge. If you have other untracked (possibly ignored) files in the directory this method won't remove them.
The solution
This snippet will extract all untracked files that would be overwritten by git pull
and delete them.
git pull 2>&1|grep -E '^\s'|cut -f2-|xargs -I {} rm -rf "{}"
and then just do:
git pull
This is not git porcelain command so always double check what it would do with:
git pull 2>&1|grep -E '^\s'|cut -f2-|xargs -I {} echo "{}"
Explanation - because one liners are scary:
Here's a breakdown of what it does:
git pull 2>&1
- capture git pull
output and redirect it all to stdout so we can easily capture it with grep
.
grep -E '^\s
- the intent is to capture the list of the untracked files that would be overwritten by git pull
. The filenames have a bunch of whitespace characters in front of them so we utilize it to get them.
cut -f2-
- remove whitespace from the beginning of each line captured in 2.
xargs -I {} rm -rf "{}"
- us xargs
to iterate over all files, save their name in "{}" and call rm
for each of them. We use -rf
to force delete and remove untracked directories.
It would be great to replace steps 1-3 with porcelain command, but I'm not aware of any equivalent.
回答10:
git merge -f
does not exit, but git checkout -f
does.
git checkout -f donor-branch
git checkout receiving-branch
git merge donor-branch
This works because you make Git force-checkout the other branch, and thereby force-replace the untracked files with their tracked versions. If you then checkout back to your original branch the tracked files disappear, like in every checkout where files don't exist in the destination branch.
You mention a pull
command in your answer. Pull is nothing but git fetch
(acquire the remote history) + an automatic merge of the upstream branch; if you precede the solution above with a git fetch
, and merge the upstream branch, the steps will look as follows.
git fetch origin
git checkout -f origin/mybranch
git checkout mybranch
git merge origin/mybranch