Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
914 views
in Technique[技术] by (71.8m points)

git diff renamed file

I have a file a.txt.

cat a.txt
> hello

The contents of a.txt is "hello".

I make a commit.

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

I then move a.txt into a test dir.

mkdir test
mv a.txt test

I then make my second commit.

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

Finally, I edit a.txt to say "goodbye" instead.

cat a.txt
> goodbye

I make my last commit.

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

Now here is my question:

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

I've tried: git diff HEAD^^..HEAD -M a.txt, but that didn't work. git log --follow a.txt properly detects the rename, but I can't find an equivalent for git diff. Is there one?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The issue with the difference between HEAD^^ and HEAD is that you have an a.txt in both commits, so just considering those two commits (which is what diff does), there is no rename, 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

Incidentally, if you restrict your diff to just one path (as you do in git diff HEAD^^ HEAD a.txt you aren't ever going to see the renames or copies because you've excluded the everything apart from a single path and renames or copies - by definition - involve two paths.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...