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

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.

Leave a Comment

tech