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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7203515/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 11:42:07  来源:igfitidea点击:

How to find a deleted file in the project commit history?

git

提问by Pedro Rolo

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?

当这个文件存在时,我怎样才能找到它的提交?

回答by Amber

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>

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

Note the caret symbol (^), which gets the checkout priorto 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

请注意插入符号 ( ^),它在标识之前获取检出,因为在<SHA>提交时文件被删除,我们需要查看前一次提交以获取已删除文件的内容

回答by Fatih Acet

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

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

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

Execute the next command to find commit id of that commit and copy the commit id

执行下一个命令以查找该提交的提交 id 并复制提交 id

git log --all -- FILEPATH

Show diff of deleted file

显示已删除文件的差异

git show COMMIT_ID -- FILE_PATH

Remember, you can write output to a file using >like

请记住,您可以使用>like将输出写入文件

git show COMMIT_ID -- FILE_PATH > deleted.diff

回答by Calaf

Suppose you want to recover a file called MyFile, but are uncertain of its path (or its extension, for that matter):

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

Prelim.: Avoid confusion by stepping to the git root

前言:通过进入 git 根目录避免混淆

A nontrivial project may have multiple directories with similar or identical names.

一个重要的项目可能有多个名称相似或相同的目录。

> 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

  1. 找到完整路径

    git log --diff-filter=D --summary | grep 删除 | 我的文件

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

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

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

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

  2. Checkout the file

  1. 确定影响该文件的所有提交

    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.

  2. 检出文件

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.

如果您选择第一个列出的提交(按时间顺序排列的最后一个,这里是 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.`

Just select the preceding (append a caret) commit:

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

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

回答by Akshay Agarwal

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 中恢复文件,请使用以下命令(注意 SHA 后面的“^”符号)

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

回答by Petur Subev

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

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

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

回答by Jason

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 用户可以从存储库根目录中传递已删除的文件名并获取历史记录:

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

If anybody, can improve the command, please do.

如果有人可以改进命令,请执行。

回答by Philip Oakley

Try using one of the viewers, such as gitkso that you can browse around the history to find that half remembered file. (use gitk --allif needed for all branches)

尝试使用其中一个查看器,例如gitk这样您就可以浏览历史记录以找到那个只记住了一半的文件。(gitk --all如果需要为所有分支使用)

回答by srghma

Summary:

概括:

  1. Step 1
  1. 第1步

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

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

  1. Step 2
  1. 第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 ; exit}')
  echo "Restoring file from commit before $last_commit"
  git checkout $last_commit^ -- $filepath
}

restore my/file_path

回答by Antonio Petricca

Here is my solution:

这是我的解决方案:

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