I would like to rename/move a project subtree in Git moving it from
/project/xyz
to
/components/xyz
If I use a plain git mv project components
, then all the commit history for the xyz project
gets lost. Is there a way to move this such that the history is maintained?
In my experience, the best way to retain the history of renames is using the command line and doing git mv <file or folder> - git seems to notice the rename and retains the file history. The intermediate temp folder is unnecessary, you can just use git mv oldfolder newfolder . Otherwise correct.
Git keeps track of changes to files in the working directory of a repository by their name. When you move or rename a file, Git doesn't see that a file was moved; it sees that there's a file with a new filename, and the file with the old filename was deleted (even if the contents remain the same).
The command `$git diff` shows you the individual changes in a file, a branch, or the whole repo if you want, depending on what you pass in. The `-M` flag stands for “detect move” or, for our purposes, detect a rename.
Git detects renames rather than persisting the operation with the commit, so whether you use git mv
or mv
doesn't matter.
The log
command takes a --follow
argument that continues history before a rename operation, i.e., it searches for similar content using the heuristics:
http://git-scm.com/docs/git-log
To lookup the full history, use the following command:
git log --follow ./path/to/file
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With