Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to move/rename files in Git and maintain their history?

Tags:

git

rename

mv

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?

like image 418
sgargan Avatar asked Feb 22 '10 22:02

sgargan


People also ask

How do I rename a directory in git without losing history?

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.

What happens if you rename a file in git?

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).

How does git know a file has been renamed?

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.


1 Answers

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 
like image 194
Troels Thomsen Avatar answered Sep 20 '22 21:09

Troels Thomsen