Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

how does git handle merging code that was moved to a different file?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...