删除已移动的文件后,Git分支发散

时间:2016-08-09 07:37:30

标签: git rebase

主存储库中的文件已被移动到子文件夹中,在我的本地服务器上我删除了该文件,并将在将来签入新位置时添加该文件。

在检查提交并执行git pull --rebase origin master时,我发生冲突,声明文件已在我的提交中被删除,并在另一个提交中重命名/移动,因此合并失败。

然后我做了git rebase --abort

现在当我做git状态时,我收到消息:

  

您的分支和origin / master已经分歧,每个都有1次和27次提交。

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

从头开始重新开始git rebase操作。

当你遇到冲突的步骤(你删除了,其他被移动), 您需要决定如何解决冲突,最好是与其他开发人员讨论后进行讨论。

如果要删除该文件,请将其删除:

git rm path/to/renamed/file

如果文件应该保留,则添加它:

git add path/to/renamed/file

path/to/renamed/filegit status给出的冲突路径。

删除或添加后,使用以下命令继续使用rebase:

git rebase --continue

请注意,如果您决定git add冲突的文件, 然后当你做git rebase --continue时, 它可能会告诉你:

No changes - did you forget to use 'git add'?
If there is nothing left to stage, chances are that something else
already introduced the same changes; you might want to skip this patch.

确实,该文件已被其他分支添加, 与你刚刚确认的git add保持联系, 没有真正添加任何新东西。 在这种情况下,请使用git rebase --skip继续变基。

答案 1 :(得分:1)

最初,提交图可能如下: enter image description here

您提交了更改,它就像: enter image description here

然后你想做一个rebase pull。首先,origin/master已更新: enter image description here

如果rebase pull成功,那么图表应该是:enter image description here

然而,由于冲突导致它失败并且你中止了它,所以它又变成了这个: enter image description here

master之后

origin/masterC分歧。如果您现在想要避免冲突,可以运行git reset HEAD^ --hard,这会使图形如下: enter image description here

然后通过masterorigin/master使用git merge @{u}更新git merge origin/masterenter image description here

现在您可以删除文件并像最初一样提交更改。最终的图表如下: enter image description here

你可以忽略D,它现在会从master的git日志中消失,虽然它会在回购中存在很长一段时间。

相关问题