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

前端 未结 4 618
花落未央
花落未央 2020-12-19 01:38

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 anyt

4条回答
  •  感情败类
    2020-12-19 02:25

    I don't think the previous answer is correct in the general case, because git doesn't really care about files - the file name is used as the basis for some heuristics, but the way git thinks about content isn't centred entirely around the idea of a file. Unlike other VCSs, git tracks content, and in this case the content happens to have moved, but it's the same content.

    As a result, git should be able to handle merges between branches even where files have been renamed, or code move between files, so depending on what exactly you've done, it will probably handle the merge fine.

    As long as the changes to X don't cause a merge conflict (which could happen if you'd changed both the original version and the renamed version), the fact that X has been moved to B shouldn't matter, and the merge result should contain the result of both changes. If there is a problem, it would indicate that git hasn't correctly tracked the code movement.

    In practice, the previous answer is probably based on personal experience when the detection machinery failed, in which case https://git.wiki.kernel.org/index.php/GitFaq#How_to_manually_resolve_conflicts_when_Git_failed_to_detect_rename.3F may be helpful.

提交回复
热议问题