在Git中查找文件删除的时间
在Git中查找文件删除的时间
我有一个有n次提交的Git仓库。
我有一个需要的文件,它以前在仓库里,但是我突然想找它,然后想“哦!那个文件去哪了?”
是否有(一系列)Git命令可以告诉我“file really_needed.txt已在第n-13次提交中被删除”?
换句话说,不用查看每个单独的提交,并且知道我的Git仓库有每个文件的每个更改,我能快速找到最后一个包含那个文件的提交,以便我能把它拿回来吗?
简短回答:
git log --full-history -- your_file
会显示你仓库历史中的所有提交,包括合并提交,其中涉及到your_file
。最后一个(顶部)提交是删除该文件的提交。
一些解释:
这里的--full-history
标志非常重要。如果没有它,当您请求文件的日志时,Git会执行“历史简化”。文档中缺乏关于这个工作原理的细节,我也缺乏用于从源代码中尝试弄清楚的勇气和韧性,但是git-log文档 至少说道:
默认模式
简化历史,以最简单的历史来解释树的最终状态。之所以最简单,是因为它在最终结果相同的情况下修剪了一些旁枝(即合并具有相同内容的分支)
当我们想要的文件的历史记录被删除时,这显然是令人担忧的,因为解释删除文件的最简单历史记录是没有历史。如果没有--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
标志。
如果要显示修改了一个文件的提交记录,即使该文件已被删除,请运行此命令:
git log --full-history -- [file path]
如果你只想看到删除该文件的最后一条提交记录,请在上述命令中加上 -1
:
git log --full-history -1 -- [file path]
还可以参见我写的文章:如何找回已删除的文件的提交记录。