如何对移动/重命名的文件执行 git diff?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5730460/
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
How to do a git diff on moved/renamed file?
提问by dr jerry
I moved a file using git mv
. Now I would like to do a diff on the new file to compare it with the old file (with the old, now non-existent name).
我使用git mv
. 现在我想对新文件做一个差异,将它与旧文件(旧的,现在不存在的名称)进行比较。
How do I do this?
我该怎么做呢?
回答by Zitrax
You need to use -M to let git autodetect the moved file when diffing. Using just git diff
as knittl mentioned does not work for me.
您需要使用 -M 来让 git 在比较时自动检测移动的文件。就像git diff
knittl 提到的那样使用对我不起作用。
So simply: git diff -M
should do it.
很简单:git diff -M
应该这样做。
The documentation for this switch is:
此开关的文档是:
-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.
回答by Jakub Nar?bski
In addition to what knittl wrote, you can always use:
git diff HEAD:./oldfilename newfilename
where HEAD:./oldfilename
means oldfilename in the last commit (in HEAD), relative to current directory.
其中HEAD:./oldfilename
表示上次提交中的旧文件名(在 HEAD 中),相对于当前目录。
If you don't have new enough git, you would have to use instead:
如果您没有足够新的 git,则必须改用:
git diff HEAD:path/to/oldfilename newfilename
回答by VonC
With git 2.9 (June 2016), you won't have to add -M
anymore. git diff
uses -M
by default.
使用 git 2.9(2016 年 6 月),您将不再需要添加-M
。默认git diff
使用-M
。
See commit 5404c11, commit 9501d19, commit a9276a6, commit f07fc9e, commit 62df1e6(25 Feb 2016) by Matthieu Moy (moy
).
(Merged by Junio C Hamano -- gitster
--in commit 5d2a30d, 03 Apr 2016)
请参阅Matthieu Moy ( ) 的commit 5404c11、commit 9501d19、commit a9276a6、commit f07fc9e、commit 62df1e6(2016 年 2 月 25 日)。(由Junio C Hamano合并-- --在提交 5d2a30d,2016 年 4 月 3 日)moy
gitster
diff
: activatediff.renames
by defaultRename detection is a very convenient feature, and new users shouldn't have to dig in the documentation to benefit from it.
Potential objections to activating rename detection are that it sometimes fail, and it is sometimes slow. But rename detection is already activated by default in several cases like "
git status
" and "git merge
", so activatingdiff.renames
does not fundamentally change the situation. When the rename detection fails, it now fails consistently between "git diff
" and "git status
".This setting does not affect plumbing commands, hence well-written scripts will not be affected.
diff
:diff.renames
默认激活重命名检测是一项非常方便的功能,新用户不必深入研究文档即可从中受益。
激活重命名检测的潜在反对意见是它有时会失败,有时会很慢。但是在“
git status
”和“git merge
”等几种情况下,默认情况下已激活重命名检测,因此激活diff.renames
并不会从根本上改变这种情况。当重命名检测失败时,它现在在“git diff
”和“git status
”之间始终失败。此设置不会影响管道命令,因此不会影响编写良好的脚本。
回答by mindriot
git diff -M
activates rename detection as others have said (and as @VonC pointed out, it is activated by default from git 2.9). But if you have a large changeset, inexact rename detection may still get turned off again. Git will display a warning like the following, which is easy to miss amidst the diff you are viewing:
git diff -M
像其他人所说的那样激活重命名检测(正如@VonC 指出的那样,它默认从 git 2.9 激活)。但是如果你有一个很大的变更集,不准确的重命名检测可能仍然会再次被关闭。Git 将显示如下警告,在您查看的差异中很容易错过:
warning: inexact rename detection was skipped due to too many files.
warning: you may want to set your diff.renameLimit variable to at least 450 and retry the command.
In that case, set the configuration option as suggested by git, for example
在这种情况下,例如按照 git 的建议设置配置选项
git config diff.renamelimit 450
and re-run your diff command.
并重新运行您的 diff 命令。
回答by Oli Studholme
For whatever reason using HEAD:./oldfilename
(or absolute path) didn't work for me, but HEAD:oldfilename
did (thanks cmn):
无论出于何种原因,使用HEAD:./oldfilename
(或绝对路径)对我都不起作用,但HEAD:oldfilename
确实如此(感谢 cmn):
git diff HEAD:oldfilename newfilename
git diff 2a80f45:oldfilename f65f3b3:newfilename
HTH
HTH
回答by knittl
simply run git diff
without any arguments, or git diff -- newfilename
. git is smart enough to compare the right files/contents (i.e. original content before rename with altered content after rename)
git diff
无需任何参数即可运行,或者git diff -- newfilename
. git 足够聪明,可以比较正确的文件/内容(即重命名前的原始内容与重命名后的更改内容)