4

Is it possible to do a massive refactor that involves moving and renaming many directories without losing Git change tracking?

Jason Plank
  • 2,336
  • 5
  • 31
  • 40
Santiago Corredoira
  • 47,267
  • 10
  • 52
  • 56

1 Answers1

6

git copes fine with moving / renaming directories, but to track changes across these renames you may need to add some extra parameters to whatever command you're using, or set a couple of config options.

The reason for this is that git just stores the state of the tree at each commit rather than the changes that took place in order to move from one state to another. If you have many files in your source tree then you may need to tell git to actively try to find any renames. Similarly, if you're interested in a particular file, you'll need to tell git explicitly to search for possible renamings in its past.

To give an example of the latter, a typical case is using git log -- filename for examining the history of a particular file. In order to tell git to also look for its history before any rename that might have occurred, you have to do:

git log --follow -- filename

As another example, the useful output of git log --stat may not include all of your renames or copies if you have many files in your tree, since it needs to check all pairs of files to do that. To force git to detect copies and renames when using git log and git diff you can set the config option diff.renameLimit to at least the number of files in your tree, and set the config option diff.renames to copies - this means to detect copies and renames.

Alternatively, if you don't want to set these as config options, you can use the -M or -C options to git log or git diff. These are described in more detail in Jakub Narębski's answer to this question:

Community
  • 1
  • 1
Mark Longair
  • 446,582
  • 72
  • 411
  • 327