本文翻译自: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 foo1 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 bar1 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 foo1 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 bar1 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 +0000Deleted foocommit ddff7a78068aefb7a4d19c82e718099cf57be694
Author: Mark Amery
Date:   Tue Jan 12 22:50:19 2016 +0000Added 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 +0000Merge branch 'newbranch'commit 77403443a13a93073289f95a782307b1ebc21162
Author: Mark Amery
Date:   Tue Jan 12 22:50:50 2016 +0000Deleted foocommit ddff7a78068aefb7a4d19c82e718099cf57be694
Author: Mark Amery
Date:   Tue Jan 12 22:50:19 2016 +0000Added foo
mark@lunchbox:~/example$ git log --full-history -- bar
commit 873ed352c5e0f296b26d1582b3b0b2d99e40d37c
Author: Mark Amery
Date:   Tue Jan 12 22:51:29 2016 +0000Deleted barcommit 7f9299a80cc9114bf9f415e1e9a849f5d02f94ec
Author: Mark Amery
Date:   Tue Jan 12 22:50:38 2016 +0000Added 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 更多信息请点击此处

查找在Git中删除文件的时间相关推荐

  1. 从git中删除文件夹

    要删除 git 中的文件夹,可以使用如下命令: git rm -r folder_name 其中,-r选项表示以递归的方式删除文件夹,folder_name表示要删除的文件夹的名称.执行这个命令后, ...

  2. 如何从 Git 存储库中删除文件?

    问: 如何从我的存储库中删除 "file1.txt"? 答1: huntsbot.com – 程序员副业首选,一站式外包任务.远程工作.创意产品分享订阅平台. 使用 git rm. ...

  3. Git基础-删除文件 rm/git rm 命令详解

    1.文章概述 本文介绍git中删除文件的操作: 主要涉及命令如下 : 1.rm 2.git rm3.git rm -f4.git rm --cached5.git rm -r : 在删除文件夹的时候, ...

  4. 从Git存储库中删除文件而不从本地文件系统中删除它

    我的初始提交包含一些日志文件. 我已将*log添加到我的.gitignore ,现在我想从我的存储库中删除日志文件. git rm mylogfile.log 将从存储库中删除文件,但也将从本地文件系 ...

  5. git中手动删除的文件如何在git中删除

    在日常开发中,我们可能或手动删除(delete键删除的)一些文件,然而我们本来应该是用git rm fileName命令删除的,但是现在我们手动删除了,那么要如何在git里面讲那些手动删除的文件删除呢 ...

  6. 从 Git 提交中删除文件

    问: 如何从最新提交中删除文件? 答1: huntsbot.com – 程序员副业首选,一站式外包任务.远程工作.创意产品分享订阅平台. 我认为这里的其他答案是错误的,因为这是一个将错误提交的文件从上 ...

  7. 拷贝文件产生副本_从Windows 10中删除文件后,文件的副本在文件历史记录中保留多长时间?...

    拷贝文件产生副本 If you have decided to make use of Windows 10's file history capabilities, how long will a ...

  8. linux git 撤销删除文件,删除文件以后,如何通过git撤销删除的文件,不提交到远端代码库...

    检查状态,看看发生了什么: $ git status On branch master Changed but not updated: (use "git add/rm ..." ...

  9. java 删除压缩zip文件_从ZIP存档中删除文件,而无需在Java或Python中解压缩 - java...

    从ZIP存档中删除文件,而无需使用Java(首选)或Python解压缩 你好 我使用包含数百个高度压缩的文本文件的大型ZIP文件.解压缩ZIP文件时,可能要花一些时间,并且很容易消耗多达20 GB的磁 ...

最新文章

  1. 在网络通讯中,如何自己分配可用的端口号和获取自己的ip地址
  2. Java EE 开发环境搭建
  3. PP模块快速入门之功能简介(二)
  4. Unity制作游戏中的场景
  5. Unix/Linux环境C编程入门教程(41) C语言库函数的文件操作详解
  6. 柯里化(Curing)
  7. 我是一名普通程序员,通过自己的努力,我的收入涨了3倍!
  8. Drools 规则语言详解(上)
  9. Linux装ntfs后内存不够,Linux_安装Ubuntu后无法使用NTFS硬盘或移动硬盘,  在安装Ubuntu系统后,存在 - phpStudy...
  10. fastadmin 后台view data-source关联报500错误问题
  11. 搭建vue项目时运行npm run dev 报错问题解决
  12. 系统调用之creat
  13. Linux学习整理-网络防火墙iptables-实践篇2
  14. IDEA配置方法类注释模板
  15. php 高斯分布,多元高斯分布完全解析
  16. 磁盘提示:使用驱动器中的光盘之前需要将其格式化怎么办?
  17. SQLServer笔试题
  18. ddos流量攻击有多少G_如何防护ddos流量攻击?
  19. QlExpress 性能优化(二)
  20. 永信至诚的平行仿真术,大潘:穿过这场“连环梦”

热门文章

  1. ORA-3136 - ORA-4030
  2. android 技术等级
  3. 从网管做到CIO---看如何提升IT人员职业价值
  4. ADO.NET与抽水的故事 系列三——抽水机—Command
  5. 自动化测试元素查找利器firepath介绍
  6. [Lydsy1806月赛] 最长公共子序列
  7. 2017-2018-2 20179205 《网络攻防技术与实践》第八周作业
  8. 【我的失败人生】1105感到自己的弱小
  9. 前端开发:模块化 — 高效重构
  10. ASP.NET AJAX Programmer's Reference : with ASP.NET 2.0 or ASP.NET 3.5