git diff 重命名文件
我有一个文件 a.txt
。
cat a.txt
> hello
a.txt
的内容是“hello”。
我做出承诺。
git add a.txt
git commit -m "first commit"
然后,我将 a.txt
移动到 test
目录中。
mkdir test
mv a.txt test
然后我进行第二次提交。
git add -A
git commit -m "second commit"
最后,我编辑 a.txt
来表示“再见”。
cat a.txt
> goodbye
我做出最后一次承诺。
git add a.txt
git commit -m "final commit"
现在我的问题是:
如何区分上次提交和第一次提交之间的 a.txt
内容?
我尝试过: git diff HEAD^^..HEAD -M a.txt
,但这不起作用。 git log --follow a.txt
正确检测重命名,但我找不到 git diff
的等效项。有吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
HEAD^^
和HEAD
之间的差异问题在于,您在两次提交中都有一个a.txt
,因此只需考虑这两次提交(这就是 diff 所做的),没有重命名,有副本和更改。要检测副本,您可以使用
-C
:结果:
顺便说一句,如果您将 diff 限制为仅一个路径(就像您在
git diff HEAD^^ HEAD a.txt
您永远不会看到重命名或副本,因为您已经排除了除单个路径之外的所有内容,并且重命名或副本(根据定义)涉及两个路径。The issue with the difference between
HEAD^^
andHEAD
is that you have ana.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
:Result:
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.要区分特定文件的重命名,请使用
-M --
(-C
也有效)。因此,如果您在上次提交中重命名了文件并更改了文件,则可以通过以下方式查看更改:
这将产生:(
添加了
// a.txt
行以帮助 git 检测重命名)如果 git 没有检测到重命名,您可以使用
-M[=n]
指定一个低相似度阈值,例如 1%:来自 git diff 文档:
To diff across a rename of a specific file, use
-M -- <old-path> <new-path>
(-C
also works).So if you both renamed and changed a file in the last commit, you can see the changes with:
This produces:
(
// a.txt
lines added to help git detect the rename)If git isn't detecting the rename, you can specify a low similarity threshold with
-M[=n]
, say 1%:From the git diff docs:
您还可以执行以下操作:
git diff rev1:file1 rev2:file2
,对于您的示例,这将是
git diff HEAD^^:./a.txt HEAD:./test/a。 txt
请注意显式的
./
——此格式否则假定路径相对于存储库的根目录。 (如果您位于存储库的根目录中,您当然可以忽略它。)这根本不依赖于重命名检测,因为用户明确说明了要比较的内容。 (因此,它在其他一些情况下也派上用场,例如在 git-svn 环境中比较不同 svn 分支之间的文件。)
You can also do:
git diff rev1:file1 rev2:file2
which, for your example, would be
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.)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 版本 1.6 及更高版本可以使用更方便的“暂存”标志:
相同的命令也可以与 git difftool 一起使用,以启动您的第三个-party 视觉 diff 工具(您必须首先配置):
如果您想对历史 Git 提交中的重命名文件进行 diff,请使用此:
或使用 difftool:
您可以检索重命名的文件更容易像这样:
结果:
If your rename commit is staged but not committed yet, you can use:
Git version 1.6 and above can use the more convenient "staged" flag:
The same commands also work with
git difftool
, to launch your third-party visual diff tool (which you have to configure first):If you want to do a diff of a renamed file, which is in a historical Git commit in the past, use this:
or using difftool:
You can retrieve the paths of the renamed file more easily like this:
Result: