Git的;代码合并后消失 [英] Git; code disappeared after merge
问题描述
我的同事在一段时间后做了一个改动 - 引入了一个新的功能 - 并且(成功地)致力于git。现在,该函数已经失踪。
My colleague did a change a while back - introduced a new function - and that was (sucessfully) committed to git. Now, though, that function has gone missing.
使用 git log --reverse
我设法找到该函数仍在代码中的最后一次提交(48d60a03)。下一个(e6f28bfd)提交(其中的函数消失)是一个合并(14158e1),但是 git show
'其中任何一个都不会揭示删除缺少代码。
Using git log --reverse
I've managed to find the last commit where that function was still in the code (48d60a03). The next (e6f28bfd) commit (where the function in question disappeared) is a merge (of 14158e1), but git show
'ing any of these does not reveal a delete of the missing code.
换句话说,代码在合并期间消失了,不会在合并的任何分支中被删除。
In other words, code has disappeared during a merge, without being deleted in either of the branches being merged.
在几个小时内搜索StackOverflow让我得出结论,在冲突解决期间它必定是一个手动错误(我是否正确?)。就这样吧,发生了。问题是 - 如何获取代码 - 除了使用缺少的代码进行新提交之外,还有其他方法吗?
Searching StackOverflow for a couple of hours leads me to conclude, that it must be a manual error during a conflict resolution (did I get that right?). So be it, that happens. Question is - how do I get that code back - is there any other way than making a new commit with the missing code?
相关问题;我能否以某种方式发现,如果有其他事情像这样再次出现?我有点担心;)
Related question; can I somehow find out, if there are other instances of stuff going bye-bye like this? I'm slightly worried ;)
推荐答案
因为您的历史记录已被推送,所以最好的方法是进行新的提交。否则,您将面临丢失其他代码和混淆每个人的回购的风险。
Because your history has already been pushed, the best way is to make a new commit. Otherwise, you run the risk of losing other code and messing up everyone's repos.
由于您知道上次提交函数的位置,因此您可以 git checkout 48d60a03 - <具有函数的文件的名称>
。然后你可以用这个函数提交旧/新文件。
Since you know where the commit that last has the function, you can git checkout 48d60a03 -- <name of file with function>
. Then you can commit the old/new file with the function.
由于文件中可能会有其他更改,因此您可能需要 git reset
将文件取消保存,并使用 git add -p
来仅添加您正在查找的函数的更改。
As there are likely to be other changes in the file, you will probably want to git reset
to unstage the file and use git add -p
to only add the changes for the function that you are looking for.
为防止这种情况发生,我建议您获得一个完整的测试套件,您可以在完成合并后运行。这可以帮助最大限度地减少代码因为测试失败而丢失的可能性。
For preventing this from happening, my recommendation is to get a comprehensive test suite that you can run after completing a merge. That can help minimize the chances that code will be lost as tests will fail.
这篇关于Git的;代码合并后消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!