git, sure-fire way to move/rename files while keeping the history

后端 未结 2 860
独厮守ぢ
独厮守ぢ 2021-02-04 02:37

I know there are \"lots\" of existing questions that looks similar, so let me summarize them before asking mine.

  • The answer to Is it possible to move/rename files
2条回答
  •  情话喂你
    2021-02-04 03:10

    tl;dr; no

    Longer version: In my experience, git is very good at detecting the move/rename as long as the file is unmodified. Git uses heuristics to attempt and locate the move. It can be fooled by having several files that are too similar, or if the file has been modified during the move, causing it to be too dissimilar from its original.

    The best way I have found to do this is to do multi-stage commits, separating all of your moves into one commit, followed by the changes in another. For example...

    git mv foo.txt bar.txt
    git commit
    
    ... modify bar.txt ...
    
    git add bar.txt
    git commit
    

    It will not guarantee your move is detected correctly, as it can still get confused when there are multiple candidates. However it has worked very well for me and catches the majority of cases.

提交回复
热议问题