tags:

views:

582

answers:

4

I've moved a bunch of files around manually without thinking, and can't find a way to get git to recognize that the files are just moved and not actually different files. Is there a way to do this other than removing old and adding the new (and thus losing the history), or redoing all the changes with git-mv?

+4  A: 

I think it already does this. Now, I could be wrong, but I've read that git tracks files based on their contents not based on their position in the file system or based on delta/differences. In the stack I think it shows it as if the files are being removed and the then re-added, but I think I've tried this once and it still maintained the history, due to the aforementioned way that git tracks things.

Still would be helpful for someone to verify if I'm correct or not. Sorry if I misunderstood your question.

Jorge Israel Peña
This is correct. There should be no difference between moving a file (and using "git rm" and "git add" on the old and new files, respectively), and using "git mv". Michael: if the problem is that you are not seeing the pre-move history in "git log FILE", try using "git log --follow FILE".
Phil
Thanks for the confirmation Phil, I appreciate it.
Jorge Israel Peña
+2  A: 

git doesn't track the history of individual files and it doesn't treat moves and copies specially, that is there is no special metadata that indicates that a move or copy occurred. Instead each git commit is a complete snapshot of the working tree.

If you want to see moves in git log you can supply -M in addition to an option that lists which files have changed, e.g.

git log --summary -M

git will look at the adjacent trees in the commit history and infer if any files where moved by each commit.

To find copies as well as renames you can use the -C option, you can supply it twice to make git look harder for possible copy sources at the expense of some performance.

git log --summary -M -C -C

Note, that as git doesn't store file history (only commit history), even if you did git rm and git mv the file, you wouldn't lose any history. All changes to the path would still be recorded and visible in a git log.

Charles Bailey
You can also set `diff.renames` config variable to `true` either in `.git/config` (repository config), or in `~/.gitconfig` (user's config). See git-config manpage for details.
Jakub Narębski
A: 

To better understand why Git does do rename detection instead of (more common) explicit rename tracking, and how git log path limiting works, you can read read Linus's ultimate content tracking tool blog post by Junio C Hamano, maintainer of Git (and references therein).

Jakub Narębski
+1  A: 

To have git remove files that are removed or moved already, just enter

git add -u
Hugo