4

I just went through a somewhat painful process of renaming/reorganizing and refactoring a large codebase on a feature branch. During the process, I did incremental checkins with few changes to ensure that git recognized the renames. However, now that I am merging this feature back into my development branch, it appears that Git does not "remember" these renames, and is treating them as remove/add instead. Setting different rename-threshold values does not seem to be helping.

Shouldn't git know from the previous commit that the files were renamed/moved?

EDIT What's odd is that if I merge dev into my feature branch, instead of vice-versa, git seems to recognize the renames. I ended up doing this, then resetting dev to the top of my feature branch. Does the direction of the merge matter in this case?

3
  • I am really interested to see answers to this - good question Sep 21, 2012 at 4:46
  • Does git recognize renames in feature branch?
    – Dima
    Sep 21, 2012 at 4:47
  • It did on the original checkin of the reorganized files, yes. Following that, subsequent code cleanup did change the content of the files in that feature branch. Sep 21, 2012 at 4:53

2 Answers 2

2

git does not actually track renames -- it relies on a 'rename detection' algorithm to detect renames, as stated on the Git FAQ:

Git has to interoperate with a lot of different workflows, for example some changes can come from patches, where rename information may not be available. Relying on explicit rename tracking makes it impossible to merge two trees that have done exactly the same thing, except one did it as a patch (create/delete) and one did it using some other heuristic.

On a second note, tracking renames is really just a special case of tracking how content moves in the tree. In some cases, you may instead be interested in querying when a function was added or moved to a different file. By only relying on the ability to recreate this information when needed, Git aims to provide a more flexible way to track how your tree is changing.

However, this does not mean that Git has no support for renames. The diff machinery in Git has support for automatically detecting renames, this is turned on by the '-M' switch to the git-diff-* family of commands. The rename detection machinery is used by git-log(1) and git-whatchanged(1), so for example, 'git log -M' will give the commit history with rename information. Git also supports a limited form of merging across renames. The two tools for assigning blame, git-blame(1) and git-annotate(1) both use the automatic rename detection code to track renames.

As a very special case, 'git log' version 1.5.3 and later has '--follow' option that allows you to follow renames when given a single path.

Thus, if you refactored and renamed a file, its similarity to its old file will be very small, and the resulting status or log may indicate that the file was deleted and then added.

A few options might help detect renames even through refactoring:

  • -M<n>

    Detect renames. If n is specified, it is a threshold on the similarity index (i.e. amount of addition/deletions compared to the file's size). For example, -M90% means git should consider a delete/add pair to be a rename if more than 90% of the file hasn't changed.

  • -w

    Ignore changes in amount of whitespace. This ignores whitespace at line end, and considers all other sequences of one or more whitespace characters to be equivalent.

  • -B<n>

    Break complete rewrite changes into pairs of delete and create.

-B is useful because it can allow files to be considered as rename sources, even if they've been changed; for example, it detects a rename if you moved 90% of foo.c to bar.c but left some functions in foo.c.

2
  • So even when I'm viewing the history of a commit, and it shows that a rename occurred, git is "detecting" that rename all over again on that commit? Sep 21, 2012 at 4:55
  • Yes. You can see this in action if you use -M<n> while using git log -p: the appearance of the commits will actually change depending on the n you choose.
    – nneonneo
    Sep 21, 2012 at 5:00
0

You are likely falling foul of the git config setting: merge.renameLimit

This setting is defaulted quite low (in comparison to large codebases) and will not attempt to detect renames if you are merging too many files, in fear of it being slow. It's quite possible that merging in one direction goes over the limit, and the other direction does not, hence the strange rename detection behaviour.

Try raising the limit by

git config merge.renameLimit 9999

and retrying.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.