在git中,您可以查看移动后应用于文件的旧提交吗?

Ben*_*off 16 git

在git(using git mv)中移动文件后,查看该文件的日志只显示包含和移动后的提交.

有没有办法查看以旧名称应用于该文件的提交?在下面的例子中,我想git log在完成移动后看到提交b04033bdc44f1和8ca40d563ce5d .

 $ git init
Initialized empty Git repository in /Users/ben/code/git_mv_example/.git/
 $ touch foo
 $ git add foo
 $ git commit -m "Initial commit"
Created initial commit 8ca40d5: Initial commit
 0 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 foo
 [master*]$ echo "abcdefg" > foo
 [master*]$ git commit -a -m "edited foo"
Created commit b04033b: edited foo
 1 files changed, 1 insertions(+), 0 deletions(-)
 $ git log foo
commit b04033bdc44f1eb3477270b4b7ca727377d8c03a
Author: Ben Brinckerhoff <ben@devver.net>
Date:   Tue Jun 2 13:26:53 2009 -0600

    edited foo

commit 8ca40d563ce5d07d965bfb75a01b9c23378fd321
Author: Ben Brinckerhoff <ben@devver.net>
Date:   Tue Jun 2 13:26:15 2009 -0600

    Initial commit
 $ git mv foo bar
 [master+]$ git commit -a -m "renamed foo to bar"
Created commit 2bccdf6: renamed foo to bar
 1 files changed, 0 insertions(+), 0 deletions(-)
 rename foo => bar (100%)
 $ git log bar
commit 2bccdf6fc65b9da5b279d9f1117e436549dd3a7b
Author: Ben Brinckerhoff <ben@devver.net>
Date:   Tue Jun 2 13:27:14 2009 -0600

    renamed foo to bar
 $ git log foo
fatal: ambiguous argument 'foo': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions
Run Code Online (Sandbox Code Playgroud)

这不可能吗?或者git log对此可能是错误的命令?

Von*_*onC 16

' git-log -M'将提供重命名信息的提交历史记录,如GitFaq中提到的"为什么git not"跟踪"重命名?"

diffGit中的机器支持自动检测重命名,这是通过' -M'切换到git-diff-*命令族来打开的.
重命名检测机器由git-log和使用git-whatchanged.
Git还支持有限形式的重命名合并.这两个工具表示谴责,git-blamegit-annotate都使用了自动重命名检测代码跟踪重命名.

作为一个非常特殊的情况,' git log'版本1.5.3及更高版本具有' --follow'选项,允许您在给定单个路径时遵循重命名.


在你的例子中:

F:\prog\git\test\rename>git log --follow bar
commit 81f52b91eb2fc7ad18051c93f3e4d583f27c15ca
Author: VonC <>
Date:   Tue Jun 2 21:54:43 2009 +0200

    renamed foo to bar

commit 71aff26ace6ab249ab2042d1e5d20377486ce478
Author: VonC <>
Date:   Tue Jun 2 21:54:19 2009 +0200

    edited foo

commit c893199da767eddac6a547b940557435ade4d18c
Author: VonC <>
Date:   Tue Jun 2 21:53:51 2009 +0200

    initial commit
Run Code Online (Sandbox Code Playgroud)

注意:所有这些都是可能的,因为foo在重命名为bar之前,您没有彻底修改内容.如果内容完全不同,该--follow选项将无法获取所有历史记录.


Jak*_*ski 5

简答:使用

$ git log -M --follow bar
Run Code Online (Sandbox Code Playgroud)

要么

$ git log -M -- foo bar
Run Code Online (Sandbox Code Playgroud)

指定旧名称和新名称.

顺便说一下,由于git使用基于启发式相似性的重命名检测,玩具示例可能不会像您想要的那样工作,而现实生活中的示例(或内容更加不变的示例)将按预期工作.

请注意,在git路径中,作为git-log的参数不是路径过滤器,而是路径限制器(例如可以是目录).