如何在项目提交历史中找到已删除的文件?

发布于 2024-12-01 10:56:04 字数 101 浏览 4 评论 0原文

曾几何时,我的项目中有一个文件,我现在希望能够获取它。

问题是:我不知道我什么时候删除了它,也不知道它在哪条路径上。

当该文件存在时,如何找到该文件的提交?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

凉城 2024-12-08 10:56:05

获取已删除文件的列表并复制已删除文件的完整路径

git log --diff-filter=D --summary | grep delete

查找该提交的提交 ID 并复制提交 ID

git log --all -- FILEPATH

显示已删除文件的差异

git show COMMIT_ID -- FILE_PATH

可以选择使用 > 将输出写入文件

git show COMMIT_ID -- FILE_PATH > deleted.diff

Get a list of the deleted files and copy the full path of the deleted file

git log --diff-filter=D --summary | grep delete

Find the commit id of that commit and copy the commit id

git log --all -- FILEPATH

Show diff of the deleted file

git show COMMIT_ID -- FILE_PATH

Optionally write output to a file using >

git show COMMIT_ID -- FILE_PATH > deleted.diff
骷髅 2024-12-08 10:56:05

假设您想要恢复一个名为 MyFile 的文件,但不确定其路径(或其扩展名):

0。(初步)通过进入 git 根目录来避免混淆

一个不平凡的项目可能会具有多个具有相似或相同文件名的目录。

> cd <project-root>
1.找到完整路径
> git log --diff-filter=D --summary | grep delete | grep MyFile

`delete mode 100644 full/path/to/MyFile.js`

full/path/to/MyFile.js就是路径&您正在寻找的文件。

2. 确定影响该文件的所有提交
> git log --oneline --follow -- full/path/to/MyFile.js

`bd8374c Some helpful commit message`

`ba8d20e Another prior commit message affecting that file`

`cfea812 The first message for a commit in which that file appeared.`
3. 签出文件

如果您选择第一个列出的提交(按时间顺序排列的最后一个,此处为 bd8374c),则将找不到该文件,因为它已在该提交中被删除。

> git checkout bd8374c -- full/path/to/MyFile.js

`error: pathspec 'full/path/to/MyFile.js' did not match any file(s) known to git.`

只需选择前面的(附加插入符号)提交:

> git checkout bd8374c^ -- full/path/to/MyFile.js

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.

> cd <project-root>
1. Find the full path
> git log --diff-filter=D --summary | grep delete | grep MyFile

`delete mode 100644 full/path/to/MyFile.js`

full/path/to/MyFile.js is the path & file you're seeking.

2. Determine all the commits that affected that file
> git log --oneline --follow -- full/path/to/MyFile.js

`bd8374c Some helpful commit message`

`ba8d20e Another prior commit message affecting that file`

`cfea812 The first message for a commit in which that file appeared.`
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.

> git checkout bd8374c -- full/path/to/MyFile.js

`error: pathspec 'full/path/to/MyFile.js' did not match any file(s) known to git.`

Just select the preceding (append a caret) commit:

> git checkout bd8374c^ -- full/path/to/MyFile.js
与之呼应 2024-12-08 10:56:05

无法编辑接受的响应,因此将其添加为此处的答案,

要在 git 中恢复文件,请使用以下命令(请注意 SHA 后面的“^”符号)

git checkout <SHA>^ -- /path/to/file

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)

git checkout <SHA>^ -- /path/to/file
茶底世界 2024-12-08 10:56:05

@Amber 给出了正确答案!再补充一点,如果您不知道文件的确切路径,可以使用通配符!这对我有用。

git log --all -- **/thefile.*

@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 log --all -- **/thefile.*
我三岁 2024-12-08 10:56:05

下面是一个简单的命令,开发人员或 git 用户可以从存储库根目录传递已删除的文件名并获取历史记录:

git log --diff-filter=D --summary | grep filename | awk '{print $4; exit}' | xargs git log --all -- 

如果有人可以改进该命令,请这样做。

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:

git log --diff-filter=D --summary | grep filename | awk '{print $4; exit}' | xargs git log --all -- 

If anybody, can improve the command, please do.

‘画卷フ 2024-12-08 10:56:05

尝试使用其中一种查看器,例如 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. (use gitk --all if needed for all branches)

堇色安年 2024-12-08 10:56:05

如果您希望查看所有已删除文件的大小

以及关联的SHA

git log --all --stat --diff-filter=D --oneline

请添加-p|--patch以查看内容要

git log --all --stat --diff-filter=D -p

缩小到任何文件,您有两个简单的选择,您可以使用路径规范,也可以通过管道连接到 grep 并搜索文件名。

使用 grep:

git log --all --stat --diff-filter=D --oneline | grep foo

使用路径规范:

git log --all --stat --diff-filter=D --oneline -- '*foo*'

如果您想查看内容,路径规范可以与 -p|--patch 很好地配合使用:

git log --all --stat --diff-filter=D --oneline --patch -- '*foo*'

如果您知道文件在哪里,您可能也会喜欢这个

git log --all --full-history -- someFileName

If you prefer to see the size of all deleted file

as well as the associated SHA

git log --all --stat --diff-filter=D --oneline

add a -p|--patch to see the contents too

git log --all --stat --diff-filter=D -p

To 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:

git log --all --stat --diff-filter=D --oneline | grep foo

Using a pathspec:

git log --all --stat --diff-filter=D --oneline -- '*foo*'

A pathspec can work well together with -p|--patch, if you want to see contents:

git log --all --stat --diff-filter=D --oneline --patch -- '*foo*'

You might also like this one if you know where the file is

git log --all --full-history -- someFileName
囚我心虐我身 2024-12-08 10:56:05

摘要:

  1. 第 1 步

在已删除文件的历史记录中搜索文件完整路径 git log --diff-filter=D --summary | grep filename

  1. 步骤 2

从删除之前的提交恢复文件

restore () {
  filepath="$@"
  last_commit=$(git log --all --full-history -- $filepath | grep commit | head -1 | awk '{print $2; exit}')
  echo "Restoring file from commit before $last_commit"
  git checkout $last_commit^ -- $filepath
}

restore my/file_path

Summary:

  1. Step 1

You search your file full path in history of deleted files git log --diff-filter=D --summary | grep filename

  1. Step 2

You restore your file from commit before it was deleted

restore () {
  filepath="$@"
  last_commit=$(git log --all --full-history -- $filepath | grep commit | head -1 | awk '{print $2; exit}')
  echo "Restoring file from commit before $last_commit"
  git checkout $last_commit^ -- $filepath
}

restore my/file_path
听不够的曲调 2024-12-08 10:56:05

这是我的解决方案:

git log --all --full-history --oneline -- <RELATIVE_FILE_PATH>
git checkout <COMMIT_SHA>^ -- <RELATIVE_FILE_PATH>

Here is my solution:

git log --all --full-history --oneline -- <RELATIVE_FILE_PATH>
git checkout <COMMIT_SHA>^ -- <RELATIVE_FILE_PATH>
淡水深流 2024-12-08 10:56:05

我遇到了这种情况,我什至不知道文件名是什么,所以我想查看所有已删除的文件...

总的来说,我强烈建议熟悉 git-filter-repo。它在重写历史记录方面有很多用途,但其分析功能之一包括非常快速地识别存储库中所有已删除的文件,并显示它们的大小和删除日期。 (在非 Windows 操作系统上使用它相当 ,并且这里是专门针对 Windows 的安装说明。)

简单明了 让它在你的路径中运行,你只需运行:

git filter-repo --analyze

这将在你的 .git 文件夹中输出一些文件,而你想在这个问题中查看的文件称为:

.git\filter-repo\analysis\path-deleted-sizes.txt

此文件显示了你的存储库中所有已删除的文件,按大小反向排序,以及删除日期。这是一个示例输出:

=== Deleted paths by reverse accumulated size ===
Format: unpacked size, packed size, date deleted, path name(s)
    57151421   44898377 2022-07-22 somdirectory/somefileA
    46034619   42929136 2022-01-18 somdirectory/somefileB
    65332368   29102439 2020-09-28 somdirectory/somefileC
    23686432   21681498 2022-07-22 somdirectory/somefileD
    23681272   21678818 2022-07-22 somdirectory/somefileE
    23676153   21670502 2022-07-22 somdirectory/somefileF
    43232768   21439037 2020-07-10 somdirectory/somefileG
    18714315   14299243 2019-01-10 somdirectory/somefileH
    16788104   13035176 2019-01-10 somdirectory/somefileI

显然,您可以使用它来查找您要查找的文件,或者,在重写历史记录的上下文中,根据第二列大小,我知道是否从历史记录中删除了这 9 个已删除的文件我将回收大约 220 MB。

一旦确定了要查找的文件,您就可以使用它来查找提交:

git log --all --full-history -- <filename>

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:

git filter-repo --analyze

This will output some files in your .git folder, and the one you want to look at for this question is called:

.git\filter-repo\analysis\path-deleted-sizes.txt

This file shows all deleted files in your repo, reverse sorted by size, and the date it was deleted. Here's an example output:

=== Deleted paths by reverse accumulated size ===
Format: unpacked size, packed size, date deleted, path name(s)
    57151421   44898377 2022-07-22 somdirectory/somefileA
    46034619   42929136 2022-01-18 somdirectory/somefileB
    65332368   29102439 2020-09-28 somdirectory/somefileC
    23686432   21681498 2022-07-22 somdirectory/somefileD
    23681272   21678818 2022-07-22 somdirectory/somefileE
    23676153   21670502 2022-07-22 somdirectory/somefileF
    43232768   21439037 2020-07-10 somdirectory/somefileG
    18714315   14299243 2019-01-10 somdirectory/somefileH
    16788104   13035176 2019-01-10 somdirectory/somefileI

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 log --all --full-history -- <filename>
茶底世界 2024-12-08 10:56:04

如果您不知道可以使用的确切路径

git log --all --full-history -- "**/thefile.*"

如果您知道文件所在的路径,则可以执行以下操作:

git log --all --full-history -- <path-to-file>

这应该显示触及该文件的所有分支中的提交列表。然后,您可以找到所需文件的版本,并使用以下命令显示它...

git show <SHA> -- <path-to-file>

或者使用以下命令将其恢复到您的工作副本中:

git checkout^ --< /code>

请注意插入符号 (^),它会在所识别的符号之前进行签出,因为在 时刻> commit 文件被删除了,我们需要查看一下在上一次提交时获取已删除文件的内容

If you do not know the exact path you may use

git log --all --full-history -- "**/thefile.*"

If you know the path the file was at, you can do this:

git log --all --full-history -- <path-to-file>

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...

git show <SHA> -- <path-to-file>

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文