git diff 重命名文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7759193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
git diff renamed file
提问by Ken Hirakawa
I have a file a.txt
.
我有一个文件a.txt
。
cat a.txt
> hello
The contents of a.txt
is "hello".
内容a.txt
是“你好”。
I make a commit.
我做出承诺。
git add a.txt
git commit -m "first commit"
I then move a.txt
into a test
dir.
然后我a.txt
进入一个test
目录。
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.
最后,我编辑a.txt
改为说“再见”。
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?
我如何区分a.txt
上次提交和第一次提交之间的内容?
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?
我试过:
git diff HEAD^^..HEAD -M a.txt
,但这没有用。 git log --follow a.txt
正确检测重命名,但我找不到git diff
. 有吗?
回答by CB Bailey
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.
HEAD^^
和之间的区别的问题HEAD
是你a.txt
在两个提交中都有一个,所以只考虑这两个提交(这是 diff 所做的),没有重命名,有一个副本和一个更改。
To detect copies, you can use -C
:
要检测副本,您可以使用-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 HEAD^^ HEAD a.txt
您将永远不会看到重命名或副本,因为您已经排除了除单个路径之外的所有内容,并且重命名或副本 - 根据定义 - 涉及两个路径。
回答by Nolan Amy
To diff across a rename of a specific file, use -M -- <old-path> <new-path>
(-C
also works).
要区分特定文件的重命名,请使用-M -- <old-path> <new-path>
(-C
也适用)。
So if you both renamed andchanged a file in the last commit, you can see the changes with:
因此,如果您在上次提交中重命名和更改了文件,则可以通过以下方式查看更改:
git diff HEAD^ HEAD -M -- a.txt test/a.txt
This produces:
这产生:
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 the rename)
(// a.txt
添加行以帮助 git 检测重命名)
If git isn't detecting the rename,you can specify a low similarity threshold with -M[=n]
, say 1%:
如果 git 没有检测到重命名,您可以使用 指定一个低相似度阈值-M[=n]
,比如 1%:
git diff HEAD^ HEAD -M01 -- a.txt test/a.txt
From the git diff docs:
-M[<n>] --find-renames[=<n>]
Detect renames. If
n
is specified, it is a threshold on the similarity index (i.e. amount of addition/deletions compared to the file's size). For example,-M90%
means Git should consider a delete/add pair to be a rename if more than 90% of the file hasn't changed. Without a%
sign, the number is to be read as a fraction, with a decimal point before it. I.e.,-M5
becomes 0.5, and is thus the same as-M50%
. Similarly,-M05
is the same as-M5%
. To limit detection to exact renames, use-M100%
. The default similarity index is 50%.
-M[<n>] --find-renames[=<n>]
检测重命名。如果
n
指定,则它是相似性索引的阈值(即与文件大小相比的添加/删除量)。例如,-M90%
如果超过 90% 的文件没有更改,则 Git 应该将删除/添加对视为重命名。没有%
符号,数字将被读作分数,在它之前有一个小数点。即,-M5
变为 0.5,因此与 相同-M50%
。同样,-M05
与 相同-M5%
。要将检测限制为精确重命名,请使用-M100%
. 默认相似度指数为 50%。
回答by benkc
You can also do:
你也可以这样做:
git diff rev1:file1 rev2:file2
git diff rev1:file1 rev2:file2
which, for your example, would be
就您而言,这将是
git diff HEAD^^:./a.txt HEAD:./test/a.txt
git diff HEAD^^:./a.txt HEAD:./test/a.txt
Note the explicit ./
-- this format otherwise assumes the paths to be relative to the root of the repo. (If you're in the root of the repo, you can of course omit that.)
注意显式./
——否则这种格式假定路径是相对于 repo 的根目录。(如果您在 repo 的根目录中,您当然可以省略它。)
This doesn't depend on the rename detection at all, as the user is explicitly stating exactly what to compare. (Therefore, it also comes in handy in some other circumstances, such as comparing files between different svn branches in a git-svn environment.)
这根本不依赖于重命名检测,因为用户明确说明了要比较的内容。(因此,它在其他一些情况下也派上用场,例如在 git-svn 环境中比较不同 svn 分支之间的文件。)
回答by Mr-IDE
If your rename commit is staged but not committed yet, you can use:
如果您的重命名提交已暂存但尚未提交,您可以使用:
git diff --cached -M -- file.txt renamed_file.txt