Record file copy operation with Git

前端 未结 2 466
悲&欢浪女
悲&欢浪女 2020-11-27 10:19

When I move a file in git using git-mv the status shows that the file has been renamed and even if I alter some portions it still considers to be almost the same thing (whic

2条回答
  •  死守一世寂寞
    2020-11-27 10:56

    2020-05-19: The following solution has the advantages of not changing the log of the original file, not creating a merge conflict, and being shorter.

    You can force Git to detect the history of the copied file in three commits:

    • Instead of copying, switch to a new branch and move the file to its new location there.
    • Re-add the original file there.
    • Merge the new branch to the original branch with the no-fast-forward option --no-ff.

    Credits to Raymond Chen. What follows is his procedure. Say the file is named orig, and you want the duplicate to be named apple:

    git checkout -b dup # create and switch to branch
    
    git mv orig apple # make the duplicate
    git commit -m "duplicate orig to apple"
    
    git checkout HEAD~ orig # bring back the original
    git commit -m "restore orig"
    
    git checkout - # switch back to source branch
    git merge --no-ff dup # merge dup into source branch
    

    The former solution had four commits:

    • Instead of copying, switch to a new branch and move the file to its new location there.
    • Switch to the original branch and rename the file.
    • Merge the new branch into the original branch, resolving the trivial conflict by keeping both files.
    • Restore the original filename in a separate commit.

    (Solution taken from https://stackoverflow.com/a/44036771/1389680.)

提交回复
热议问题