如何在项目提交历史中找到已删除的文件?
曾几何时,我的项目中有一个文件,我现在希望能够获取它。
问题是:我不知道我什么时候删除了它,也不知道它在哪条路径上。
当该文件存在时,如何找到该文件的提交?
Once upon a time, there was a file in my project that I would now like to be able to get.
The problem is: I have no idea of when have I deleted it and on which path it was.
How can I locate the commits of this file when it existed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
获取已删除文件的列表并复制已删除文件的完整路径
查找该提交的提交 ID 并复制提交 ID
显示已删除文件的差异
可以选择使用
>
将输出写入文件Get a list of the deleted files and copy the full path of the deleted file
Find the commit id of that commit and copy the commit id
Show diff of the deleted file
Optionally write output to a file using
>
假设您想要恢复一个名为
MyFile
的文件,但不确定其路径(或其扩展名):0。(初步)通过进入 git 根目录来避免混淆
一个不平凡的项目可能会具有多个具有相似或相同文件名的目录。
1.找到完整路径
full/path/to/MyFile.js
就是路径&您正在寻找的文件。2. 确定影响该文件的所有提交
3. 签出文件
如果您选择第一个列出的提交(按时间顺序排列的最后一个,此处为 bd8374c),则将找不到该文件,因为它已在该提交中被删除。
只需选择前面的(附加插入符号)提交:
Suppose you want to recover a file called
MyFile
, but are uncertain of its path (or its extension, for that matter):0. (Preliminary) Avoid confusion by stepping to the git root
A nontrivial project may have multiple directories with similar or identical filenames.
1. Find the full path
full/path/to/MyFile.js
is the path & file you're seeking.2. Determine all the commits that affected that file
3. Checkout the file
If you choose the first-listed commit (the last chronologically, here bd8374c), the file will not be found, since it was deleted in that commit.
Just select the preceding (append a caret) commit:
无法编辑接受的响应,因此将其添加为此处的答案,
要在 git 中恢复文件,请使用以下命令(请注意 SHA 后面的“^”符号)
Could not edit the accepted response so adding it as an answer here,
to restore the file in git, use the following (note the '^' sign just after the SHA)
@Amber 给出了正确答案!再补充一点,如果您不知道文件的确切路径,可以使用通配符!这对我有用。
@Amber gave correct answer! Just one more addition, if you do not know the exact path of the file you can use wildcards! This worked for me.
下面是一个简单的命令,开发人员或 git 用户可以从存储库根目录传递已删除的文件名并获取历史记录:
如果有人可以改进该命令,请这样做。
Below is a simple command, where a dev or a git user can pass a deleted file name from the repository root directory and get the history:
If anybody, can improve the command, please do.
尝试使用其中一种查看器,例如 gitk,这样您就可以浏览历史记录以找到那个只记得一半的文件。 (如果所有分支都需要,请使用 gitk --all )
Try using one of the viewers, such as
gitk
so that you can browse around the history to find that half remembered file. (usegitk --all
if needed for all branches)如果您希望查看所有已删除文件的
大小
以及关联的
SHA
,请添加
-p|--patch
以查看内容要缩小到任何文件,您有两个简单的选择,您可以使用路径规范,也可以通过管道连接到 grep 并搜索文件名。
使用 grep:
使用路径规范:
如果您想查看内容,路径规范可以与
-p|--patch
很好地配合使用:如果您知道文件在哪里,您可能也会喜欢这个
If you prefer to see the
size
of all deleted fileas well as the associated
SHA
add a
-p|--patch
to see the contents tooTo narrow down to any file you have two easy options, you can use a pathspec or you can pipe to grep and search for file name.
Using grep:
Using a pathspec:
A pathspec can work well together with
-p|--patch
, if you want to see contents:You might also like this one if you know where the file is
摘要:
在已删除文件的历史记录中搜索文件完整路径
git log --diff-filter=D --summary | grep filename
从删除之前的提交恢复文件
Summary:
You search your file full path in history of deleted files
git log --diff-filter=D --summary | grep filename
You restore your file from commit before it was deleted
这是我的解决方案:
Here is my solution:
我遇到了这种情况,我什至不知道文件名是什么,所以我想查看所有已删除的文件...
总的来说,我强烈建议熟悉 git-filter-repo。它在重写历史记录方面有很多用途,但其分析功能之一包括非常快速地识别存储库中所有已删除的文件,并显示它们的大小和删除日期。 (在非 Windows 操作系统上使用它相当 ,并且这里是专门针对 Windows 的安装说明。)
简单明了 让它在你的路径中运行,你只需运行:
这将在你的 .git 文件夹中输出一些文件,而你想在这个问题中查看的文件称为:
此文件显示了你的存储库中所有已删除的文件,按大小反向排序,以及删除日期。这是一个示例输出:
显然,您可以使用它来查找您要查找的文件,或者,在重写历史记录的上下文中,根据第二列大小,我知道是否从历史记录中删除了这 9 个已删除的文件我将回收大约 220 MB。
一旦确定了要查找的文件,您就可以使用它来查找提交:
I had this happen where I didn't even know what the file's name was, so I wanted to see all deleted files...
In general, I highly recommend becoming familiar with git-filter-repo. It has many uses for re-writing history, but one of its analysis features includes very quickly identifying all deleted files in a repo, and displaying their sizes and date deleted. (Using it on non-Windows OSes is fairly straight-forward, and here are installation instructions specifically for Windows.)
Once you have it runnable in your path, you simply run:
This will output some files in your .git folder, and the one you want to look at for this question is called:
This file shows all deleted files in your repo, reverse sorted by size, and the date it was deleted. Here's an example output:
Obviously you can use this to find the file you're looking for, or, in the context of re-writing history, based on the 2nd column sizes, I know if I remove those 9 deleted files from the history I'll reclaim about 220 MB.
Once you've identified the file you're looking for you can use this to find the commits:
如果您不知道可以使用的确切路径
如果您知道文件所在的路径,则可以执行以下操作:
这应该显示触及该文件的所有分支中的提交列表。然后,您可以找到所需文件的版本,并使用以下命令显示它...
或者使用以下命令将其恢复到您的工作副本中:
git checkout^ --< /code>
请注意插入符号 (
^
),它会在所识别的符号之前进行签出,因为在
时刻> commit 文件被删除了,我们需要查看一下在上一次提交时获取已删除文件的内容If you do not know the exact path you may use
If you know the path the file was at, you can do this:
This should show a list of commits in all branches which touched that file. Then, you can find the version of the file you want, and display it with...
Or restore it into your working copy with:
git checkout <SHA>^ -- <path-to-file>
Note the caret symbol (
^
), which gets the checkout prior to the one identified, because at the moment of<SHA>
commit the file is deleted, we need to look at the previous commit to get the deleted file's contents