在 Git 中查找文件何时被删除
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6839398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find when a file was deleted in Git
提问by JohnMetta
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 命令会告诉我“文件really_needed.txt 在提交n-13 时被删除”?
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 存储库对每个文件进行了每次更改,我能否快速找到包含该文件的最后一次提交,以便将其取回?
回答by vogella
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]
显示文件的变化,即使文件被删除也能工作。
Example:
例子:
git log --full-history -- myfile
If you want to see only the last commit, which deleted a file use -1 in addition, e.g., git log --full-history -1 -- [file path]
如果您只想查看删除文件的最后一次提交,请另外使用 -1,例如, git log --full-history -1 -- [file path]
回答by Mark Amery
Short answer:
简短的回答:
git log --full-history -- your_file
will show you allcommits in your repo's history, including merge commits, that touched your_file
. The last (top) one is the one that deleted the file.
将显示您的回购历史记录中的所有提交,包括合并提交,触及your_file
. 最后一个(顶部)是删除文件的那个。
Some explanation:
一些解释:
The --full-history
flag here is important. Without it, Git performs "history simplification" when you ask it for the log of a file. The docs are light on details about exactly how this works and I lack the grit and courage required to try to figure it out from the source code, but the git-log docshave this much to say:
--full-history
这里的旗帜很重要。没有它,当您向 Git 询问文件日志时,它会执行“历史简化”。文档对具体如何工作的细节很清楚,我缺乏尝试从源代码中弄清楚它所需的勇气和勇气,但是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 (i.e. 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? Unfortunately, yes. Here's a demonstration:
这显然与我们想要其历史记录的文件何时被删除有关,因为解释已删除文件的最终状态的最简单的历史记录是没有历史记录。是否有git log
没有--full-history
会简单地声称该文件从未创建过的风险?不幸的是,是的。这是一个演示:
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 is "simplifying" history down into a fiction where bar
never existed. git log --full-history -- bar
, on the other hand, gives us the commit that created bar
and the commit that deleted it.
请注意git log -- bar
上面的终端转储如何导致字面上没有输出;Git 正在将历史“简化”成一个bar
从未存在过的虚构故事。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. History simplification might sometimes be helpful when you're trying to understand how a currently-existingfile came to be in its current state, but when trying to track down a file deletionit'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
标志,因为git log -- some_file
我在尝试跟踪已删除文件的真实存储库中失败了。当您试图了解当前存在的文件是如何处于当前状态时,历史简化有时可能会有所帮助,但是当试图追踪文件删除时,它更有可能通过隐藏您关心的提交来搞砸. 始终在--full-history
此用例中使用该标志。
回答by daniel
Git log but you need to prefix the path with --
Git 日志,但您需要在路径前加上 --
Eg:
例如:
dan-mac:test dani$ git log file1.txt
fatal: ambiguous argument 'file1.txt': unknown revision or path not in the working tree.
dan-mac:test dani$ git log -- file1.txt
commit 0f7c4e1c36e0b39225d10b26f3dea40ad128b976
Author: Daniel Palacio <[email protected]>
Date: Tue Jul 26 23:32:20 2011 -0500
foo
回答by estani
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 中有没有办法列出存储库中所有已删除的文件?)用于使用正则表达式查找已删除文件的提交:
git log --diff-filter=D --summary | sed -n '/^commit/h;/\/some_dir\//{G;s/\ncommit \(.*\)/ /gp}'
This returns everything deleted within a directory named some_dir
(cascading). Any sed regexp there where \/some_dir\/
is will do.
这将返回名为some_dir
(级联)的目录中删除的所有内容。那里的任何 sed regexp\/some_dir\/
都可以。
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 \(.*\)/ /gp' -e }
回答by Akif
回答by Eric Woodruff
Try:
尝试:
git log --stat | grep file