3 ms·
I sometimes wish Git properly records copy and move information. It turns out Git's heuristics for detecting copies and moves work about 95% of the time, and fo
by kccqzy 7y ago
I sometimes wish Git properly records copy and move information. It turns out Git's heuristics for detecting copies and moves work about 95% of the time, and for the remaining 5% it's mildly annoying to read a git blame with every line from a move. You can blame further across that move, but that's manual. If copy and move information is perfectly recorded, I'd have more incentive to do these kinds of code reorganization.
- ryanianian 7y agoThis article mentioned a while ago had relevant info: https://news.ycombinator.com/item?id=22689301 https://news.ycombinator.com/item?id=22689301 In my experience, ensure you don't change the code as you move it, don't rename files and move content at the same time, and don't move "too much" code in a single commit. Around 2k lines at a time seems to be a good number. Maybe some languages/structures are easier for git to analyze when doing blames.
- kccqzy 7y agoHow do you not change the code as it's being moved, when you need to update #include directives, imports, and other file paths?
- ryanianian 7y agoSometimes it is unavoidable, but in general minimize it. Don't refactor a class and rename it at the same time. Hopefully moves just result in imports and paths changing, something that is not likely to confuse a blame (and rarely do I care about the blame lines on #includes/imports)
- derefr 7y agoIt's easy if not every commit needs to compile. :)
- vimax 7y agoNew branch, make two commits. Squash merge the branch as a single commit.
- moojd 7y ago'git mv <source> <dest>' works pretty well for this.
- kccqzy 7y agoIt does not. That's the same as doing a normal mv followed by git add. It does not record the fact that you moved it. You can verify this by moving a file and making big changes to it in the same commit. Git will forget that you moved it.
- TechBro8615 7y agoIf you `git mv`, then immediately commit (without making any additional changes), I think it will recognize it as a move 100% of the time (i.e., will not use its "similarity index"). I'm not certain of that though.
- darekkay 7y agoThat's correct, but this also applies to "move file" + "git add". Just make sure to commit the "move" and "change" parts separately.
- hansvm 7y agoSure, but if you mv and git add without making any additional changes and then immediately commit, it'll also get picked up as a move 100% of the time.