Git diff renamed file

I have a.txt file.

 cat a.txt > hello 

The contents of a.txt are "hello."

I am doing a commit.

 git add a.txt git commit -m "first commit" 

Then move a.txt to the test directory.

 mkdir test mv a.txt test 

Then I do my second commit.

 git add -A git commit -m "second commit" 

Finally, I edit a.txt to say goodbye.

 cat a.txt > goodbye 

I am making the last commit.

 git add a.txt git commit -m "final commit" 

Now here is my question:

How do I distinguish the contents of a.txt between my last commit and my first commit?

I tried: git diff HEAD^^..HEAD -M a.txt , but this did not work. git log --follow a.txt correctly defines renaming, but I cannot find an equivalent for git diff . There is one?

+82
git diff rename
Oct 13 '11 at 19:08
source share
3 answers

The problem with the difference between HEAD^^ and HEAD is that you have a.txt in both commits, so just by looking at these two commits (this is what diff does), there is no renaming, there is a copy and a change.

To detect copies, you can use -C :

 git diff -C HEAD^^ HEAD 

Result:

 index ce01362..dd7e1c6 100644 --- a/a.txt +++ b/a.txt @@ -1 +1 @@ -hello +goodbye diff --git a/a.txt b/test/a.txt similarity index 100% copy from a.txt copy to test/a.txt 

By the way, if you restrict your diff to only one path (as you do in git diff HEAD^^ HEAD a.txt , you will never see the rename or copies, because you excluded everything separately from the same path and renamed or copied - by definition - use two ways.

+94
Oct 13 '11 at 19:26
source share

To change the renaming of a specific file, use -M -- <old-path> <new-path> ( -C ).

So, if you both renamed and changed the file in the last commit, you can see the changes with:

 git diff HEAD^ HEAD -M -- a.txt test/a.txt 

This gives:

 diff --git a/a.txt b/test/a.txt similarity index 55% rename from a.txt rename to test/a.txt index 3f855b5..949dd15 100644 --- a/a.txt +++ b/test/a.txt @@ -1,3 +1,3 @@ // a.txt -hello +goodbye 

( // a.txt lines added to help git detect renaming)




If git does not detect renaming, you can specify a threshold with a low level of similarity with -M[=n] , say 1%:

 git diff HEAD^ HEAD -M01 -- a.txt test/a.txt 

From git diff docs :

-M [<n>] --find-renames [= <n>]

Rename Detection. If n specified, this is the threshold value for the similarity index (i.e. the amount of addition / deletion compared to the file size). For example, -M90% means git should consider the pair delete / add as renaming if more than 90% of the file has not changed. An unsigned % number must be read as accurate to a decimal point. Ie, -M5 becomes 0.5, and thus coincides with -M50% . Similarly, -M05 same as -M5% . To limit detection for accurate renaming, use -M100% . By default, affinity index is 50%.

+61
Mar 10 '13 at 20:32
source share

You can also do:

git diff rev1:file1 rev2:file2

which for your example will be

git diff HEAD^^:./a.txt HEAD:./test/a.txt

Pay attention to the explicit ./ - this format otherwise assumes that the paths will refer to the root of the repo. (If you are at the root of the repo, you can certainly omit this.)

This is independent of rename detection at all, as the user explicitly indicates what to compare. (Therefore, it will also come in handy in some other circumstances, such as comparing files between different svn branches in a git-svn environment.)

+18
May 14 '15 at 21:32
source share



All Articles