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

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

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

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

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

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


当前回答

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

当你想合并时:

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

其他回答

一种方法是存储本地更改并从远程回购中提取。这样,你就不会丢失你的本地文件,因为文件会去隐藏。

git add -A
git stash
git pull

你可以使用git stash list这个命令检查你的本地存储文件

删除所有未跟踪的文件:

git clean  -d  -fx .

警告:这将删除IDE文件和任何有用的文件,只要你不跟踪这些文件。请谨慎使用此命令

对于那些不知道的人,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 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

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

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