Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git status / git diff -C not detecting file copy in index

Tags:

git

The documentation for git status [1] implies that it should be able to detect renames and copies (with the C state) or no matter what git diff -C should do it, but neither appear to work:

mkdir test
cd test/
git init
echo 'Hello World!' > hello.txt
echo 'Goodbye World!' > goodbye.txt
git add -A
git commit -m "Initial commit"

cp hello.txt copied.txt
mv goodbye.txt moved.txt
git add -A

$ git status --short
A  copied.txt  <------------ NO COPY DETECTED
R  goodbye.txt -> moved.txt

$ git diff -M -C --summary --cached
 create mode 100644 copied.txt  <------------ NO COPY DETECTED
 rename goodbye.txt => moved.txt (100%)

$ git commit -m Test
$ git diff -M -C --summary HEAD~
  create mode 100644 copied.txt  <------------ NO COPY DETECTED
  rename goodbye.txt => moved.txt (100%)

Side related question: is it possible to have git status or git diff detect copies and renames in the workdir prior to adding the changes to the index?

[1] http://git-scm.com/docs/git-status

like image 226
Pol Avatar asked Oct 24 '25 02:10

Pol


1 Answers

Since nobody on SO apparently knew the answer to the main question about git status, I ended up asking the question on the official Git mailing list as well [1]:

About git status not detecting the file copy (the doc is actually wrong, it just doesn't detect copies at all):

git-status has used renames since mid-2005. The documentation mentioning copies was added much later, along with the short and porcelain formats. That code handles whatever the diff engine throws at it. I don't think anybody considered at that time the fact that you cannot actually provoke status to look for copies.

About git diff -C not detecting the file copy (you need to also pass --find-copies-harder):

By default, -C only finds copies when the source file was also modified in the same commit. Since you did not modify hello.txt in the same commit where you copied it to copied.txt, it will not be considered.

If you pass -C -C (twice), or use --find-copies-harder, Git will consider all files in the repository. Note that this can be slower, which is the reason why it's not the default.

[1] http://marc.info/?l=git&m=141730775928542&w=2

like image 127
Pol Avatar answered Oct 25 '25 20:10

Pol



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!