Home > OS >  show copied file in git log / git show
show copied file in git log / git show

Time:10-01

I got the following output after committing my changes:

[master 1d8c20c] Refactor config BCs
 5 files changed, 130 insertions( ), 424 deletions(-)
 create mode 100644 kustomize/templates/build-config.yml
 rewrite kustomize/templates/build.yml (78%)
 copy kustomize/templates/{build.yml => common-build.yml} (61%)

But if I now use git log or git show it does not show the copy/rewrite part, how do I get git to display it in git log or git show?

Tried so far:

  1. Simple git log -- only commit message, no file status
  2. git log --summary / git show --summary -- shows copy/rewrite as create

CodePudding user response:

The --summary option to git log causes git log to run git diff --summary at the end. This is also what git commit does for this case, so that's why you want the --summary. But: git diff, by default, does not look for copies of files. To make git diff look to see if, perhaps, a file might have been copied to make a new one, you must pass the -C flag (optionally, more than once or with --find-copies-harder).

The --summary diff that git commit runs includes the -C option by default, at least in your particular version of Git (this has changed over time so different Git versions behave slightly differently here).1 Hence, to get git log --summary or git show --summary the same way that your git commit showed it, add -C to your command's options.

Note that, as this implies, the output of git diff changes based on which options you provide. Git didn't store the changes! Git stored the old versions of all of your files in the old commit, and then stored the new versions of all of your files in the new commit. (This is how Git can produce, on demand, all the files from any historical commit.) The comparison commands—git log -p, git log --summary, git show, git diff, and so on—extract the two commits' snapshots and then compare them. The comparison is done right then, using whatever additional options you choose right then, and those options affect the visible result, which is only computed right when you ask for it. All Git really has is the stored snapshots (plus some metadata) in each commit.


1You can also change some defaults with certain git config settings. For instance, git config --global diff.renames true enables rename detection, even in older versions of Git (pre-Git-2.9) where it is disabled by default. You can enable copy detection by default by setting diff.renames to copy, if you like.

  •  Tags:  
  • git
  • Related