Search code examples
gitgit-diffgit-diff-tree

Why does git diff-tree show always show rename as add and delete


In two test commits, wherein the first had only the operation git mv file moved_file, and the second only mv moved_file moved_again_file && git add -A moved_file moved_again_file, both appear as renames in git status before commit, but in git commit --verbose they show as new and removed files.

I'm expecting git diff-tree HEAD after each commit to show one R (rename) status but I keep getting two: an A and a D.

I'm trying to write a script that will perform actions based on file statuses but I can't properly plan for R (or C) statuses if they always show up as add/delete.

Git is version 1.8.3.1, if that makes a difference.


Solution

  • The git diff-tree command is a plumbing command, not a porcelain command, so that it has predictable behavior.

    One of these is that rename detection is off unless explicitly turned on. Contrast this with, e.g., git diff, which is a porcelain command and therefore sets rename detection based on your personal configuration for diff.renames.

    To get rename detection turned on, add -M or --find-renames (and their optional thresholds). Use -C or --find-copies (with their optional thresholds) to find copies; add --find-copies-harder if desired.