git如何处理移动到不同文件的合并代码? [英] how does git handle merging code that was moved to a different file?
问题描述
我在git中找到的关于移动代码的大多数文章都提到重命名整个文件而不是文件内的代码块。我找到的最接近的是从Linus的kerneltrap :
使用git时,整个'保持
代码移动与
分离'有一个更基本的原因:
git可以跟踪代码移动(同样,
是移动整个文件还是仅移动文件之间的
函数),并且执行
'git blame -C'实际上会跟随文件之间的
代码移动。它通过相似性分析确实是
,但它的
表示如果您同时移动
代码和更改它,
git无法看到'哦,那个函数
原来是来自另一个文件',
,现在你得到更糟的注释
关于代码实际起源的地方。
blockquote>
所以看起来git会认识到代码已经移动到其他地方,但并没有真正说出合并过程中发生了什么。
不,git不会做这种改变。当你移动整个文件时它会注意到;所以,举例来说,如果您移动了文件,删除了其中的所有内容,那么它可能有机会接受更改。但它不会对每个子文件或任何类型的重构进行更改。
Say I have function X in file A, and I wanted to move that function to file B. In the meantime, somebody else made changes to function X in file A.
Does git do anything special when merging those two changes? Would it recognize the same function was moved to file B and apply the changes there? Or would we end up losing the changes or having two copies of the same function in file A and B?
Most of the articles I found about moving code in git refer mostly to renaming whole files and not blocks of code inside files. The closest I found is a blurb from Linus at kerneltrap:
And when using git, the whole 'keep code movement separate from changes' has an even more fundamental reason: git can track code movement (again, whether moving a whole file or just a function between files), and doing a 'git blame -C' will actually follow code movement between files. It does that by similarity analysis, but it does mean that if you both move the code and change it at the same time, git cannot see that 'oh, that function came originally from that other file', and now you get worse annotations about where code actually originated.
So it seems like git will recognize that the code was moved somewhere else, but doesn't really say what happens during a merge.
解决方案No, git won't do this level of change. It will notice when you move an entire file; so, for example, if you moved the file, deleted everything else in it, then it might stand a chance of picking up the changes. But it doesn't do a change on a per-subfile or any kind of refactoring.
这篇关于git如何处理移动到不同文件的合并代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文