简答:
git log --full-history -- your_file
将向您显示您的存储库历史中的所有次提交,包括涉及your_file 的合并提交。最后一个(顶部)是删除文件的那个。
一些解释:
这里的--full-history 标志很重要。没有它,当你向它询问文件的日志时,Git 会执行“历史简化”。文档详细说明了它的工作原理,我缺乏尝试从源代码中找出答案所需的勇气和勇气,但the git-log docs 有很多话要说:
默认模式
将历史简化为解释树最终状态的最简单历史。最简单,因为如果最终结果相同(即合并具有相同内容的分支),它会修剪一些侧分支
这显然与我们想要其历史记录的文件何时被删除有关,因为解释已删除文件的最终状态的最简单历史记录是无历史记录。没有--full-history 的git log 是否会简单地声称该文件从未创建过?不幸的是,是的。这是一个演示:
mark@lunchbox:~/example$ git init
Initialised empty Git repository in /home/mark/example/.git/
mark@lunchbox:~/example$ touch foo && git add foo && git commit -m "Added foo"
[master (root-commit) ddff7a7] Added foo
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 foo
mark@lunchbox:~/example$ git checkout -b newbranch
Switched to a new branch 'newbranch'
mark@lunchbox:~/example$ touch bar && git add bar && git commit -m "Added bar"
[newbranch 7f9299a] Added bar
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 bar
mark@lunchbox:~/example$ git checkout master
Switched to branch 'master'
mark@lunchbox:~/example$ git rm foo && git commit -m "Deleted foo"
rm 'foo'
[master 7740344] Deleted foo
1 file changed, 0 insertions(+), 0 deletions(-)
delete mode 100644 foo
mark@lunchbox:~/example$ git checkout newbranch
Switched to branch 'newbranch'
mark@lunchbox:~/example$ git rm bar && git commit -m "Deleted bar"
rm 'bar'
[newbranch 873ed35] Deleted bar
1 file changed, 0 insertions(+), 0 deletions(-)
delete mode 100644 bar
mark@lunchbox:~/example$ git checkout master
Switched to branch 'master'
mark@lunchbox:~/example$ git merge newbranch
Already up-to-date!
Merge made by the 'recursive' strategy.
mark@lunchbox:~/example$ git log -- foo
commit 77403443a13a93073289f95a782307b1ebc21162
Author: Mark Amery
Date: Tue Jan 12 22:50:50 2016 +0000
Deleted foo
commit ddff7a78068aefb7a4d19c82e718099cf57be694
Author: Mark Amery
Date: Tue Jan 12 22:50:19 2016 +0000
Added foo
mark@lunchbox:~/example$ git log -- bar
mark@lunchbox:~/example$ git log --full-history -- foo
commit 2463e56a21e8ee529a59b63f2c6fcc9914a2b37c
Merge: 7740344 873ed35
Author: Mark Amery
Date: Tue Jan 12 22:51:36 2016 +0000
Merge branch 'newbranch'
commit 77403443a13a93073289f95a782307b1ebc21162
Author: Mark Amery
Date: Tue Jan 12 22:50:50 2016 +0000
Deleted foo
commit ddff7a78068aefb7a4d19c82e718099cf57be694
Author: Mark Amery
Date: Tue Jan 12 22:50:19 2016 +0000
Added foo
mark@lunchbox:~/example$ git log --full-history -- bar
commit 873ed352c5e0f296b26d1582b3b0b2d99e40d37c
Author: Mark Amery
Date: Tue Jan 12 22:51:29 2016 +0000
Deleted bar
commit 7f9299a80cc9114bf9f415e1e9a849f5d02f94ec
Author: Mark Amery
Date: Tue Jan 12 22:50:38 2016 +0000
Added bar
请注意上面终端转储中的git log -- bar 是如何导致字面上没有输出的; Git 正在将历史“简化”成bar 从未存在过的小说。另一方面,git log --full-history -- bar 为我们提供了创建 bar 的提交和删除它的提交。
要明确:这个问题不仅仅是理论上的。我只查看了文档并发现了--full-history 标志,因为git log -- some_file 在我试图追踪已删除文件的真实存储库中失败了。当您试图了解 当前存在的 文件如何进入其当前状态时,历史简化有时可能会有所帮助,但在尝试追踪文件时删除隐藏你关心的提交更有可能把你搞砸。在此用例中始终使用 --full-history 标志。