在我的分支上,我在.gitignore中有一些文件

在另一个分支上,这些文件不是。

我想将不同的分支合并到我的分支中,我不关心这些文件是否不再被忽略。

不幸的是,我得到了这个:

以下未跟踪的工作树文件将被合并覆盖

我如何修改我的pull命令来覆盖这些文件,而不需要我自己找到、移动或删除这些文件?


当前回答

如果你考虑使用-f标志,你可以先运行它作为一个演练。你只需要提前知道你接下来会遇到什么样有趣的情况;-P

-n 
--dry-run 
    Don’t actually remove anything, just show what would be done.

其他回答

问题是,当我们有传入的更改,将合并未跟踪的文件,git抱怨。这些命令帮助了我:

git clean -dxf
git pull origin master

除了接受的答案,你当然可以删除文件,如果他们不再需要通过指定文件:

git clean -f '/path/to/file/'

如果你想知道git clean会删除哪些文件,记得先用-n标志运行它。注意,这些文件将被删除。对我来说,我并不关心他们,所以这对我来说是一个更好的解决方案。

对于那些不知道的人,git忽略了文件和文件夹中的大写/小写名称差异。当您用不同的情况将它们重命名为完全相同的名称时,结果是一场噩梦。

当我将文件夹从“Petstore”重命名为“Petstore”(大写到小写)时遇到了这个问题。我已经编辑了我的.git/config文件以停止忽略大小写,进行了更改,压缩了我的提交,并保存了我的更改以移动到不同的分支。我不能将我所存储的更改应用到另一个分支。

The fix that I found that worked was to temporarily edit my .git/config file to temporarily ignore case again. This caused git stash apply to succeed. Then, I changed ignoreCase back to false. I then added everything except for the new files in the petstore folder which git oddly claimed were deleted, for whatever reason. I committed my changes, then ran git reset --hard HEAD to get rid of those untracked new files. My commit appeared exactly as expected: the files in the folder were renamed.

我希望这能帮助你避免我同样的噩梦。

唯一对我有用的命令是: (请注意,这会删除所有本地文件)

git fetch --all
git reset --hard origin/{{your branch name}}

安全地删除/覆盖麻烦的文件

当你想合并时:

git checkout -f donor-branch   # replace bothersome files with tracked versions
git checkout receiving-branch  # tracked bothersome files disappear
git merge donor-branch         # merge works

当你想拉的时候:

git fetch
git checkout -f origin/mybranch   # replace bothersome files with tracked versions
git checkout mybranch             # tracked bothersome files disappear
git pull origin/mybranch          # pull works

这就是你使用它所需要知道的。下面是一个解释。


详细解释

我们要删除的烦人文件:

存在于捐赠分支(对于git拉:上游分支), 在接收分支中不存在, 并且正在阻止合并,因为它们在你的工作目录中存在并且未被跟踪。

Git merge -f和Git pull -f不存在,但是Git checkout -f存在。

我们将使用git checkout -f + git checkout来跟踪+删除麻烦的文件,然后您的合并可以正常进行。

步骤1。这一步强制将未跟踪的Bothersome Files替换为跟踪的捐赠分支版本(它还检出捐赠分支,并更新工作目录的其余部分)。

git checkout -f donor-branch

步骤2。这一步删除了麻烦文件,因为它们在我们当前(捐赠)分支中被跟踪,而在我们切换到的接收分支中不存在。

git checkout receiving-branch

步骤3。现在Bothersome Files不存在了,在捐赠分支中合并将不会覆盖任何未跟踪的文件,因此我们不会得到错误。

git merge donor-branch