git 以下未跟踪的工作树文件将被合并覆盖,但我不在乎
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17404316/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The following untracked working tree files would be overwritten by merge, but I don't care
提问by CQM
On my branch I had some files in .gitignore
在我的分支上,我在 .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?
我将如何修改我的 pull 命令来覆盖这些文件,而不必自己查找、移动或删除这些文件?
回答by userFog
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.
这将跟踪所有文件,删除对这些文件的所有本地更改,然后从服务器获取文件。
回答by sKhan
You can try command to clear the untracked files from the local
您可以尝试命令从本地清除未跟踪的文件
Git 2.11 and newer versions:
Git 2.11 及更新版本:
git clean -d -f .
Older versions of Git:
旧版本的 Git:
git clean -d -f ""
Where -d
can be replaced with the following:
哪里-d
可以用以下替换:
-x
ignored files are also removed as well as files unknown to Git.-d
remove untracked directories in addition to untracked files.-f
is required to force it to run.
-x
忽略的文件以及 Git 未知的文件也会被删除。-d
除了未跟踪的文件外,还删除未跟踪的目录。-f
需要强制它运行。
Here is the linkthat can be helpful as well.
这是也可以提供帮助的链接。
回答by Asaf Manassen
The only commands that worked for me were:
对我有用的唯一命令是:
git fetch --all
git reset --hard origin/{{your branch name}}
回答by Esteis
A replacement for git merge
that will overwrite untracked files
替代git merge
它会覆盖未跟踪的文件
The comments below use 'FOI' for the 'files of interest', the files that
下面的评论使用“FOI”作为“感兴趣的文件”,这些文件
- exist in the donor branch,
- do not exist in the receiving branch,
- and are blocking the merge because they are present and untracked in your working directory.
- 存在于捐助者分支中,
- 不存在于接收分支中,
- 并且正在阻止合并,因为它们在您的工作目录中存在且未被跟踪。
git checkout -f donor-branch # replace FOI with tracked `donor` versions
git checkout receiving-branch # FOI are not in `receiving`, so they disapppear
git merge donor-branch # now the merge works
A replacement for git pull
that will overwrite untracked files
替代git pull
它会覆盖未跟踪的文件
pull = fetch + merge
, so we do git fetch
followed by the git checkout -f, git checkout, git merge
trick above.
pull = fetch + merge
,所以我们 git fetch
遵循git checkout -f, git checkout, git merge
上面的技巧。
git fetch origin # fetch remote commits
git checkout -f origin/mybranch # replace FOI with tracked upstream versions
git checkout mybranch # FOI are not in mybranch, so they disapppear
git merge origin/mybranch # Now the merge works. fetch + merge completes the pull.
Detailed explanation
详细说明
git merge -f
does not exist, but git checkout -f
does.
git merge -f
不存在,但git checkout -f
存在。
We will use git checkout -f
+ git checkout
to remove the Files Of Interest (see above), and then your merge can proceed normally.
我们将使用git checkout -f
+git checkout
删除感兴趣的文件(见上文),然后您的合并可以正常进行。
Step 1.This step forcibly replaces untracked FOI with tracked versions of the donor branch (it also checks out the donor branch, and updates the rest of the working dir).
第 1 步。这一步用捐赠者分支的跟踪版本强行替换未跟踪的 FOI(它还检查捐赠者分支,并更新工作目录的其余部分)。
git checkout -f donor-branch
Step 2.This step removes the FOI because they they are tracked in our current (donor) branch, and absent in the receiving-branch
we switch to.
第 2 步。此步骤删除 FOI,因为它们在我们当前的(捐助者)分支中被跟踪,而在receiving-branch
我们切换到的分支中不存在。
git checkout receiving-branch
Step 3.Now that the FOI are absent, merging in the donor branch will not overwrite any untracked files, so we get no errors.
第 3 步。现在 FOI 不存在,在捐助者分支中合并不会覆盖任何未跟踪的文件,因此我们不会出错。
git merge donor-branch
回答by Abhishek Goel
Remove all untracked files:
删除所有未跟踪的文件:
git clean -d -fx .
回答by mnagel
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.
如果这是一次性操作,您可以在执行拉取操作之前从工作目录中删除所有未跟踪的文件。阅读如何从当前 Git 工作树中删除本地(未跟踪)文件?有关如何删除所有未跟踪文件的信息。
Be sure to not accidentally remove untracked file that you still need ;)
确保不要意外删除您仍然需要的未跟踪文件;)
回答by Amr Mohammed
You can try that command
你可以试试那个命令
git clean -df
回答by matt
Update - a better version
更新 - 更好的版本
This tool (https://github.com/mklepaczewski/git-clean-before-merge) will:
这个工具(https://github.com/mklepaczewski/git-clean-before-merge)将:
- delete untracked files that are identical to their
git pull
equivalents, - revert changes to modified files who's modified version is identical to their
git pull
equivalents, - report modified/untracked files that differ from their
git pull
version, - the tool has the
--pretend
option that will not modify any files.
- 删除与其
git pull
等效文件相同的未跟踪文件, - 将更改还原到修改后的文件,修改后的版本与其
git pull
等价物相同, - 报告与其
git pull
版本不同的修改/未跟踪文件, - 该工具具有
--pretend
不修改任何文件的选项。
Old version
旧版本
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
并删除它们。
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 Ceramic 命令,因此请务必仔细检查它的用途:
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
- capturegit pull
output and redirect it all to stdout so we can easily capture it withgrep
.grep -E '^\s
- the intent is to capture the list of the untracked files that would be overwritten bygit 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 "{}"
- usxargs
to iterate over all files, save their name in "{}" and callrm
for each of them. We use-rf
to force delete and remove untracked directories.
git pull 2>&1
- 捕获git pull
输出并将其全部重定向到标准输出,以便我们可以轻松地使用grep
.grep -E '^\s
- 目的是捕获将被覆盖的未跟踪文件的列表git pull
。文件名前面有一堆空白字符,所以我们利用它来获取它们。cut -f2-
- 从 2 中捕获的每一行的开头删除空格。xargs -I {} rm -rf "{}"
- 我们xargs
遍历所有文件,将它们的名称保存在“{}”中并调用rm
它们中的每一个。我们-rf
用来强制删除和移除未跟踪的目录。
It would be great to replace steps 1-3 with porcelain command, but I'm not aware of any equivalent.
用瓷器命令替换步骤 1-3 会很棒,但我不知道有任何等价物。
回答by MMM
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.
如果您想查看 git clean 将删除哪些文件,请记住首先使用 -n 标志运行它。请注意,这些文件将被删除。就我而言,无论如何我都不关心它们,所以这对我来说是一个更好的解决方案。
回答by Maarten
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
如果您考虑使用该-f
标志,您可能首先将其作为试运行运行。只是你事先知道你接下来会遇到什么样的有趣情况;-P
-n
--dry-run
Don't actually remove anything, just show what would be done.