git diff重命名文件 [英] git diff renamed file

查看:136
本文介绍了git diff重命名文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件 a.txt

  cat a .txt 
> hello

a.txt 的内容是hello。

我做了一个提交。

  git add a.txt 
git commit -mfirst commit

然后移动 a.txt 转换为 test dir。

  mkdir test 
mv a.txt test

然后让我的第二个

  git add -A 
git commit -m第二次提交

最后,我编辑 a.txt 来表示再见 p>

  cat a.txt 
>再见

我做了最后一次提交。

  git add a.txt 
git commit -mfinal commit

现在这是我的问题:

如何区分 a.txt 在我最后一次提交和我第一次提交之间?

我试过了:
git diff HEAD ^^ .. HEAD -M a.txt ,但那并不奏效。 git log --follow a.txt 正确检测到重命名,但找不到相同的 git diff 。有没有一个?

解决方案

HEAD 是你在两次提交中都有一个 a.txt ,所以只考虑这两个提交什么差异),没有重命名,有一个副本和一个变化。

要检测副本,您可以使用 -C code $:

  git diff -C HEAD ^^ HEAD 

结果:

 索引ce01362..dd7e1c6 100644 
--- a / a.txt
+++ b / a.txt
@@ -1 +1 @@
-hello
+ goodbye
diff --git a / a.txt b / test / a.txt
相似性索引100%
从a.txt复制
复制到测试/ a.txt

顺便说一句,如果你将diff限制为只有一个路径(就像你在 git diff HEAD ^ ^ HEAD a.txt 您不会看到重命名或副本,因为您排除了单一路径以外的所有内容并重命名或警察ies - 根据定义 - 涉及两条路径。

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?

解决方案

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.

这篇关于git diff重命名文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆