查找在Git中删除文件的时间

本文翻译自:Find when a file was deleted in Git

I have a Git repository with n commits. 我有一个带有n次提交的Git存储库。

I have a file that I need, and that used to be in the repository, and that I suddenly look for and think "Oh! Where'd that file go?" 我有一个我需要的文件,而且曾经存放在存储库中,我突然想到并想“哦!那个文件去哪了?”

Is there a (series of) Git command(s) that will tell me that "file really_needed.txt was deleted at commit n-13"? 是否有(系列)Git命令会告诉我“在提交n-13时删除了文件really_needed.txt”?

In other words, without looking at every individual commit, and knowing that my Git repo has every change of every file, can I quickly find the last commit that HAS that file, so I can get it back? 换句话说,如果不查看每个单独的提交,并且知道我的Git repo对每个文件进行了每次更改,我是否可以快速找到该文件的最后一次提交,以便我可以将其恢复?


#1楼

参考:https://stackoom.com/question/ShEs/查找在Git中删除文件的时间


#2楼

I've just added a solution here (is there a way in git to list all deleted files in the repository?) for finding the commits of deleted files by using a regexp: 我刚刚在这里添加了一个解决方案(在git中是否有一种方法可以列出存储库中所有已删除的文件?) ,以便使用regexp查找已删除文件的提交:

git log --diff-filter=D --summary | sed -n '/^commit/h;/\/some_dir\//{G;s/\ncommit \(.*\)/ \1/gp}'

This returns everything deleted within a directory named some_dir (cascading). 这将返回名为some_dir (级联)的目录中删除的所有内容。 Any sed regexp there where \\/some_dir\\/ is will do. 那里有\\/some_dir\\/任何sed正则表达式都可以。

OSX (thanks to @triplee and @keif) OSX (感谢@triplee和@keif)

git log --diff-filter=D --summary | sed -n -e '/^commit/h' -e '\:/:{' -e G -e 's/\ncommit \(.*\)/ \1/gp' -e }

#3楼

git log --full-history -- [file path] shows the changes of a file, work even if the file was deleted. git log --full-history -- [file path]显示git log --full-history -- [file path]的更改,即使文件被删除也能正常工作。

Example: 例:

git log --full-history  -- myfile

If you want to see only the last commit, which deleted a file use -1 in addition, eg, git log --full-history -1 -- [file path] 如果你想只看到最后一次提交,删除文件另外使用-1,例如, git log --full-history -1 -- [file path]

See Which commit deleted a file 请参阅哪个提交删除了文件


#4楼

Short answer: 简短回答:

git log --full-history -- your_file

will show you all commits in your repo's history, including merge commits, that touched your_file . 将显示您的repo历史记录中的所有提交,包括触及your_file合并提交。 The last (top) one is the one that deleted the file. 最后一个(顶部)是删除文件的那个。

Some explanation: 一些解释:

The --full-history flag here is important. 这里的--full-history标志很重要。 Without it, Git performs "history simplification" when you ask it for the log of a file. 没有它,Git会在您询问文件日志时执行“历史简化”。 The docs are light on details about exactly how this works and I lack the grit and courage required to try and figure it out from the source code, but the git-log docs have this much to say: 文档详细介绍了它的确切工作方式,我缺乏尝试从源代码中弄清楚所需的勇气和勇气,但是git-log文档有很多话要说:

Default mode 默认模式

Simplifies the history to the simplest history explaining the final state of the tree. 将历史简化为最简单的历史,解释树的最终状态。 Simplest because it prunes some side branches if the end result is the same (ie merging branches with the same content) 最简单的,因为如果最终结果相同(即合并具有相同内容的分支),它会修剪一些侧分支

This is obviously concerning when the file whose history we want is deleted , since the simplest history explaining the final state of a deleted file is no history . 删除我们想要的历史记录的文件时,这显然是有意义的 ,因为解释已删除文件的最终状态的最简单历史记录不是历史记录 Is there a risk that git log without --full-history will simply claim that the file was never created? 没有--full-history git log是否会声称该文件从未创建过? Unfortunately, yes. 不幸的是,是的。 Here's a demonstration: 这是一个演示:

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

Notice how git log -- bar in the terminal dump above resulted in literally no output; 注意上面的终端转储中的git log -- bar如何导致字面上没有输出; Git is "simplifying" history down into a fiction where bar never existed. Git是“简化”史册成小说,其中bar根本不存在。 git log --full-history -- bar , on the other hand, gives us the commit that created bar and the commit that deleted it. 另一方面, git log --full-history -- bar为我们提供了创建bar的提交以及删除它的提交。

To be clear: this issue isn't merely theoretical. 需要明确的是:这个问题不仅仅是理论问题。 I only looked into the docs and discovered the --full-history flag because git log -- some_file was failing for me in a real repository where I was trying to track a deleted file down. 我只查看了文档并发现了--full-history标志,因为git log -- some_file在我试图跟踪已删除的文件的真实存储库中失败了。 History simplification might sometimes be helpful when you're trying to understand how a currently-existing file came to be in its current state, but when trying to track down a file deletion it's more likely to screw you over by hiding the commit you care about. 当您试图了解当前存在的文件如何处于当前状态时,历史简化有时可能会有所帮助,但是当尝试追踪文件删除时 ,更有可能通过隐藏您关心的提交来阻止您。 Always use the --full-history flag for this use case. 始终对此用例使用--full-history标志。


#5楼

这在OSX上对我有用:

git log --diff-filter=D --summary | sed -n -e '/^commit/h' -e '\:/:{' -e G -e 's/\ncommit \(.*\)/ \1/gp' -e ‘}’

#6楼

You can find the last commit which deleted file as follows: 您可以找到删除文件的最后一次提交,如下所示:

git rev-list -n 1 HEAD -- [file_path]

Further information is available here 更多信息请点击此处

发布了0 篇原创文章 · 获赞 3 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/p15097962069/article/details/105360913
今日推荐