git 吉特;合并后代码消失
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17527276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Git; code disappeared after merge
提问by Vonsild
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。但是,现在该功能已经丢失。
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.
使用git log --reverse
我设法找到该函数仍在代码中的最后一次提交(48d60a03)。下一个 (e6f28bfd) 提交(有问题的函数消失了)是一个合并(14158e1),但其中git show
任何一个都不会显示丢失代码的删除。
In other words, code has disappeared during a merge, without being deleted in either of the branches being merged.
换句话说,代码在合并过程中消失了,在合并的任何一个分支中都没有被删除。
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?
搜索 StackOverflow 几个小时让我得出结论,这一定是解决冲突期间的手动错误(我做对了吗?)。就这样吧,那会发生。问题是 - 我如何取回该代码 - 除了使用丢失的代码进行新提交之外,还有其他方法吗?
Related question; can I somehow find out, if there are other instances of stuff going bye-bye like this? I'm slightly worried ;)
相关问题;我能以某种方式找出,是否还有其他像这样再见的事例?我有点担心;)
采纳答案by Schleis
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.
因为你的历史已经被推送了,最好的方法是进行新的提交。否则,您将面临丢失其他代码并弄乱每个人的存储库的风险。
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 checkout 48d60a03 -- <name of file with function>
. 然后您可以使用该函数提交旧/新文件。
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.
由于文件中可能有其他更改,您可能希望git reset
取消暂存文件并用于git add -p
仅添加您正在查找的函数的更改。
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.
为了防止这种情况发生,我的建议是获得一个全面的测试套件,您可以在完成合并后运行它。这有助于最大程度地减少由于测试失败而丢失代码的机会。
回答by alexakarpov
changes which were committed at some point are hard to lose completely. Try running 'git reflog' and see if you can spot the commit you've lost. Then you can merge it into the current branch by running "git merge [SHA1 of your lost commit]".
在某个时候提交的更改很难完全丢失。尝试运行 'git reflog' 并查看是否可以发现丢失的提交。然后您可以通过运行“git merge [SHA1 of your lost commit]”将其合并到当前分支中。
回答by alxndr
If you know an identifiable section of the code which has been lost, you may be able to find the exact merge which lost your code by using git log -G regexToSearchFor -m --patch
如果您知道丢失的代码的可识别部分,您可以使用以下命令找到丢失代码的确切合并 git log -G regexToSearchFor -m --patch
-G <regex>
tells Git to "Look for differences whose patch text contains added/removed lines that match<regex>
"-m
tells Git to include merge commits--patch
tells Git to show a diff of whatever it finds
-G <regex>
告诉 Git“查找补丁文本包含添加/删除的匹配行的差异<regex>
”-m
告诉 Git 包含合并提交--patch
告诉 Git 显示它找到的任何东西的差异
回答by LeGEC
Apparently (see this question), git show
is not the right tool to use to have a clear view of the changes introduced by a merge commit.
显然(请参阅此问题),git show
这不是用于清楚了解合并提交引入的更改的正确工具。
Use git diff
. The merge commit has two parents :
使用git diff
. 合并提交有两个父项:
* 8ac6131 (M) Merge branch 'B'
|\
| * 5a53959 (B) two
|
* 7cb5a06 (A) one
Use git diff A M
and git diff B M
to view the differences between the merge commit and either of its parent. You should see your missing function there.
使用git diff A M
和git diff B M
查看合并提交与其任一父项之间的差异。您应该在那里看到您缺少的功能。
回答by Prince John Wesley
Related question; can I somehow find out, if there are other instances of stuff going bye-bye like this? I'm slightly worried ;)
相关问题;我能以某种方式找出,是否还有其他像这样再见的事例?我有点担心;)
Try git whatchanged. For instance,
试试git whatchanged。例如,
git whatchanged --since="2 weeks ago" -- gitk
Show the changes during the last two weeks to the file gitk.
显示过去两周内对文件 gitk.conf 的更改。
回答by gabn88
I had exactly the same. I had a new branch, a sort of version 2 of the product. Then I wanted to merge the current master (version 1 of the product) into version 2. During this merge a lot of new code on the version 1 of the product is lost. It's very frustrating, as the are tens of files with lost code and I have to go over it by hand :(
我有完全一样的。我有一个新分支,类似于产品的第 2 版。然后我想将当前的母版(产品的版本 1)合并到版本 2。在合并过程中,产品版本 1 上的许多新代码丢失了。这非常令人沮丧,因为有数十个文件丢失了代码,我必须手动检查它:(
Okay, turns out I was looking at two different folders (both on master) and forgot to pull the latest changes into one of them.
好吧,原来我正在查看两个不同的文件夹(都在主文件夹上)并且忘记将最新的更改放入其中一个。