好的,下面是故事。我们有一个包含20-30万行代码的git存储库。我的同事在repo中复制(复制,而不是移动)了一些文件来创建另一个模块。这些文件的内容只是略有修改。然后他做出了承诺。与此同时,我处理了他复制的一些文件(原件),并委托了我的工作。当我拉的时候,我意识到我没有的文件中有冲突,因为他只是创建了它们(复制了它们)。解决冲突后,我检查了我的本地repo,但我没有一半的文件。我知道git跟踪文件的内容,而不是文件名,但现在它没有太大帮助。
git log remote_repo --stat --diff-filter=D
显示删除的文件很少,
git log remote_repo --stat -C --diff-filter=D
显示删除的文件较少(但仍有一些)
我们需要两份文件,那么我们该怎么办?
您需要重新设置基础,然后再次尝试解决冲突。这可能很简单,也可能稍微复杂一些。如果很简单,您只需要重新设置合并的基础并修复已删除的文件。试试这个:
git rebase -i HEAD~1
您将看到一个屏幕,询问您想对每次提交做什么。它将类似于:
pick bb009b7 merge commit
# Rebase 9775146..bb009b7 onto 9775146
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#
更改pick以编辑、保存并退出编辑器。您现在又回到了合并提交中。运行:
git checkout <id of the previous commit> <filename>
git commit --amend
git rebase --continue
这应该会解决问题。如果没有,你需要更进一步,它可能会更复杂。
我猜它会变成这样:
# rebase the last three commits, assuming:
# the oldest is your most recent changes
# the next is your co-worker's
# the last is the merge
# You can probably go back only two but I generally
# go back one extra just to see that things are as I
# expect.
git rebase -i HEAD~3
您将看到一个屏幕,询问您想对每次提交做什么。它将类似于:
pick 8d25cf0 Your changes
pick e9ddd29 His changes
pick bb009b7 merge
# Rebase 9775146..bb009b7 onto 9775146
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#
保持第一行不变。更改第二行中的pick以进行编辑。最后一个我想你也想编辑。
写下并保存此信息,然后将开始重新创建基础。它将贯穿第一次提交,并在第二次提交时停止。你的文件应该不见了。您现在想要运行:
git checkout <id of the previous commit> <filename>
这将恢复被同事删除的文件。
然后运行:
git commit --amend
git rebase --continue
当您进行合并提交时,只需检查以确保事情如您所期望的那样。如果文件丢失,请尝试再次检出。然后修改并继续。你现在应该拥有你需要的一切。
如果你觉得任何时候都出了问题
git rebase --abort
这将使您回到开始的位置,您可以再试一次。