tags:

views:

1371

answers:

1

When I move a file in git using git-mv the status shows that the file has been renamed and even if I alter some portions it still considers to be almost the same thing (which is good because it lets me follow the history of it).

When I copy a file the original file has some history I'd like to associate with the new copy.

I have tried moving the file then trying to re-checkout in the original location - once moved git won't let me checkout the original location.

I have tried doing a filesystem copy and then adding the file - git lists it as a new file.

Is there any way to make git record a file copy operation in a similar way to how it records a file rename/move where the history can be traced back to the original file?

+11  A: 

Git does not do rename tracking not copy tracking, which means it doesn't record renames or copies. What it does instead is rename and copy detection. You can request rename detection in "git diff" (and "git show") by using '-M' option, you can request additional copy detection in changed files by using '-C' option ('-C' implies '-M'), and you can request more expensive copy detection among all files using '--find-copies-harder' or '-C -C' (which implies '-C', which implies '-M'). See git-diff manpage.

You can also configure git to always do rename detection by setting diff.renames to boolean true value (e.g. "true" or "1"), and you can request to do by default also copy detection by setting it to "copy" or "copies". See git-config manpage.

Check also '-l' option to "git diff" and related config variable diff.renameLimit.


Note that "git log <pathspec>" works differently in Git: here "<pathspec>" is set of path delimiters, where path can be a (sub)directory name. It filters and simplifies history before rename and copy detection comes into play. If you want to follow renames and copies, use "git log --follow <filename>" (which currently is a bit limited, and works only for single file).

Jakub Narębski
I'm a bit confused about the advice on config you have in the second paragraph. Would you mind clarifying that? I'm guessing what you said is not what you meant.
allyourcode
@allyourcode: What you are confused about? To turn on copy detection by default you set `diff.renames` to `copies` (e.g. '`git config diff.renames copies`'). I agree that it is a bit counterintuitive.
Jakub Narębski
One section I can't seem to parse is "and you can request to do by default also rename detection". Are you saying there's four values that diff.renames can use (true, 1, copy, copies), and that they all do the same thing?
allyourcode
@allyourcode: I'm sorry, I haven't noticed this. Fixed now, thanks.
Jakub Narębski