如何在 Git 存储库中查找并恢复已删除的文件?

发布于 2024-07-22 07:34:56 字数 230 浏览 16 评论 0 原文

假设我在 Git 存储库中。 我删除一个文件并提交该更改。 我继续工作并做出更多承诺。 然后,我发现删除该文件后需要恢复该文件。

我知道我可以使用 git checkout 签出文件 -- filename.txt,但我不知道该文件何时被删除。

  1. 如何找到删除给定文件名的提交?
  2. 如何将该文件恢复到我的工作副本中?

Say I'm in a Git repository. I delete a file and commit that change. I continue working and make some more commits. Then, I discover that I need to restore that file after deleting it.

I know I can checkout a file using git checkout <commit> -- filename.txt, but I don't know when that file was deleted.

  1. How do I find the commit that deleted a given filename?
  2. How do I restore that file back into my working copy?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(30

那片花海 2024-07-29 07:34:56

查找影响给定路径的最后一次提交。 由于该文件不在 HEAD 提交中,因此之前的提交一定已将其删除。

git rev-list -n 1 HEAD -- <file_path>

然后使用脱字号 (^) 符号检查之前提交时的版本:

git checkout <deleting_commit>^ -- <file_path>

或者在一个命令中,如果 $file 是有问题的文件。

git checkout $(git rev-list -n 1 HEAD -- "$file")^ -- "$file"

如果您使用 zsh 并启用了 EXTENDED_GLOB 选项,则插入符号将不起作用。 您可以使用 ~1 代替。

git checkout $(git rev-list -n 1 HEAD -- "$file")~1 -- "$file"

Find the last commit that affected the given path. As the file isn't in the HEAD commit, that previous commit must have deleted it.

git rev-list -n 1 HEAD -- <file_path>

Then checkout the version at the commit before, using the caret (^) symbol:

git checkout <deleting_commit>^ -- <file_path>

Or in one command, if $file is the file in question.

git checkout $(git rev-list -n 1 HEAD -- "$file")^ -- "$file"

If you are using zsh and have the EXTENDED_GLOB option enabled, the caret symbol won't work. You can use ~1 instead.

git checkout $(git rev-list -n 1 HEAD -- "$file")~1 -- "$file"
一影成城 2024-07-29 07:34:56
  1. 获取所有已删除文件的提交,以及已删除的文件:

    git log --diff-filter=D --summary 
      

    记下所需的提交哈希,例如e4e6d4d5e5c59c69f3bd7be2

  2. 将已删除的文件从之前的一次提交 (~1) 恢复到上面确定的提交 (e4e6d4d5e5c59c69f3bd7be2):

    git checkout e4e6d4d5e5c59c69f3bd7be2~1 path/to/file.ext 
      

    注意~1波形符规范将为您提供指定提交的第 n 个祖父母。

  1. Get all the commits which have deleted files, as well as the files that were deleted:

    git log --diff-filter=D --summary
    

    Make note of the desired commit hash, e.g. e4e6d4d5e5c59c69f3bd7be2.

  2. Restore the deleted file from one commit prior (~1) to the commit that was determined above (e4e6d4d5e5c59c69f3bd7be2):

    git checkout e4e6d4d5e5c59c69f3bd7be2~1 path/to/file.ext
    

    Note the ~1. The tilde spec will give you the nth grandparent of the named commit.

够运 2024-07-29 07:34:56

要恢复文件夹中所有已删除的文件:

git ls-files -d | xargs git checkout --

To restore all deleted files in a folder:

git ls-files -d | xargs git checkout --
梦里泪两行 2024-07-29 07:34:56

如果您删除了最新 HEAD 提交中存在的文件,您可以使用以下命令恢复它:

git checkout HEAD -- path/to/file.ext

If you deleted a file that exists in the latest HEAD commit, you can restore it using:

git checkout HEAD -- path/to/file.ext
〆一缕阳光ご 2024-07-29 07:34:56

如果你疯了,请使用 git-一分为二。 要做的事情如下:

git bisect start
git bisect bad
git bisect good <some commit where you know the file existed>

现在是运行自动化测试的时候了。 如果 foo.bar 存在,shell 命令'[ -e foo.bar ]' 将返回 0,否则返回 1。 git-bisect 的“run”命令将使用二分搜索自动查找测试失败的第一个提交。 它从给定范围的中间(从好到坏)开始,并根据指定测试的结果将其减半。

git bisect run '[ -e foo.bar ]'

现在您位于删除它的提交处。 从这里,您可以跳回到未来并使用 git-revert 撤消更改,

git bisect reset
git revert <the offending commit>

或者您可以返回一次提交并手动检查损坏情况:

git checkout HEAD^
cp foo.bar /tmp
git bisect reset
cp /tmp/foo.bar .

If you’re insane, use git-bisect. Here's what to do:

git bisect start
git bisect bad
git bisect good <some commit where you know the file existed>

Now it's time to run the automated test. The shell command '[ -e foo.bar ]' will return 0 if foo.bar exists, and 1 otherwise. The "run" command of git-bisect will use binary search to automatically find the first commit where the test fails. It starts halfway through the range given (from good to bad) and cuts it in half based on the result of the specified test.

git bisect run '[ -e foo.bar ]'

Now you're at the commit which deleted it. From here, you can jump back to the future and use git-revert to undo the change,

git bisect reset
git revert <the offending commit>

or you could go back one commit and manually inspect the damage:

git checkout HEAD^
cp foo.bar /tmp
git bisect reset
cp /tmp/foo.bar .
不打扰别人 2024-07-29 07:34:56

我最喜欢的新别名,基于 bonyiii答案(已投票),以及我自己关于“将参数传递给 Git 别名命令":

git config alias.restore '!f() { git checkout $(git rev-list -n 1 HEAD -- $1)~1 -- $(git diff --name-status $(git rev-list -n 1 HEAD -- $1)~1 | grep '^D' | cut -f 2); }; f'

我丢失了一个文件,在几次提交前被错误删除了?
快:

git restore my_deleted_file

危机避免。

警告,Git 2.23(2019 年第 3 季度)推出了名为 实验命令 .com/docs/git-restore" rel="nofollow noreferrer">git 恢复(!)。
因此重命名这个别名(如下所示)。


罗伯特·戴利提出在评论中以下别名:

restore-file = !git checkout $(git rev-list -n 1 HEAD -- "$1")^ -- "$1"

jegan 添加 在评论中

为了从命令行设置别名,我使用了以下命令:

git config --global alias.restore "\!git checkout \$(git rev-list -n 1 HEAD -- \"\$1\")^ -- \"\$1\"" 

My new favorite alias, based on bonyiii's answer (upvoted), and my own answer about "Pass an argument to a Git alias command":

git config alias.restore '!f() { git checkout $(git rev-list -n 1 HEAD -- $1)~1 -- $(git diff --name-status $(git rev-list -n 1 HEAD -- $1)~1 | grep '^D' | cut -f 2); }; f'

I have lost a file, deleted by mistake a few commits ago?
Quick:

git restore my_deleted_file

Crisis averted.

Warning, with Git 2.23 (Q3 2019) comes the experimental command named git restore(!).
So rename this alias (as shown below).


Robert Dailey proposes in the comments the following alias:

restore-file = !git checkout $(git rev-list -n 1 HEAD -- "$1")^ -- "$1"

And jegan adds in the comments:

For setting the alias from the command line, I used this command:

git config --global alias.restore "\!git checkout \$(git rev-list -n 1 HEAD -- \"\$1\")^ -- \"\$1\"" 
卖梦商人 2024-07-29 07:34:56

如果您知道文件名,这是使用基本命令的简单方法:

列出该文件的所有提交。

git log -- path/to/file

最后一次提交(最顶层)是删除该文件的提交。 所以你需要恢复倒数第二个提交。

git checkout {second to last commit} -- path/to/file

If you know the filename, this is an easy way with basic commands:

List all the commits for that file.

git log -- path/to/file

The last commit (topmost) is the one that deleted the file. So you need to restore the second to last commit.

git checkout {second to last commit} -- path/to/file
花辞树 2024-07-29 07:34:56

恢复已删除并提交的文件:

git reset HEAD some/path
git checkout -- some/path

已在 Git 版本 1.7.5.4 上进行测试。

To restore a deleted and commited file:

git reset HEAD some/path
git checkout -- some/path

It was tested on Git version 1.7.5.4.

中性美 2024-07-29 07:34:56

我有这个解决方案

  1. 使用以下方法之一获取删除文件的提交 ID。

    • git log --grep=*word*
    • git log -Sword
    • git 日志 | grep --context=5 *单词*
    • <代码>git log --stat | grep --context=5 *word* # 如果你很难的话推荐
      记住任何事情
  2. 你应该得到类似的东西:

提交 bfe68bd117e1091c96d2976c99b3bcc8310bebe7 作者:Alexander
奥尔洛夫日期: 2011 年 5 月 12 日星期四 23:44:27
+0200

替换了已弃用的 GWT 类 
  - gwtI18nKeySync.sh,一个过时的(?,被 Maven 目标取代)I18n 生成脚本 
  

提交 3ea4e3af253ac6fd1691ff6bb89c964f54802302 作者:Alexander
奥尔洛夫日期: 2011 年 5 月 12 日星期四 22:10:22
+0200

3。 现在使用提交 ID bfe68bd117e1091c96d2976c99b3bcc8310bebe7 执行以下操作:

git checkout bfe68bd117e1091c96d2976c99b3bcc8310bebe7^1 yourDeletedFile.java

由于提交 ID 引用文件已被删除的提交,因此您需要引用 bfe68b 之前的提交,您可以通过附加 ^1 来完成。 这意味着:给我 bfe68b 之前的提交。

I've got this solution.

  1. Get the id of the commit where the file was deleted using one of the ways below.

    • git log --grep=*word*
    • git log -Sword
    • git log | grep --context=5 *word*
    • git log --stat | grep --context=5 *word* # recommended if you hardly
      remember anything
  2. You should get something like:

commit bfe68bd117e1091c96d2976c99b3bcc8310bebe7 Author: Alexander
Orlov Date: Thu May 12 23:44:27 2011
+0200

replaced deprecated GWT class
- gwtI18nKeySync.sh, an outdated (?, replaced by a Maven goal) I18n generation script

commit 3ea4e3af253ac6fd1691ff6bb89c964f54802302 Author: Alexander
Orlov Date: Thu May 12 22:10:22 2011
+0200

3. Now using the commit id bfe68bd117e1091c96d2976c99b3bcc8310bebe7 do:

git checkout bfe68bd117e1091c96d2976c99b3bcc8310bebe7^1 yourDeletedFile.java

As the commit id references the commit where the file was already deleted you need to reference the commit just before bfe68b which you can do by appending ^1. This means: give me the commit just before bfe68b.

陈年往事 2024-07-29 07:34:56

如果您只进行了更改并删除了文件,但没有提交它,现在您放弃了更改,

git checkout -- .

但删除的文件没有返回,您只需执行以下命令:

git checkout <file_path>

很快,您的文件就回来了。

If you only made changes and deleted a file, but not commit it, and now you broke up with your changes

git checkout -- .

but your deleted files did not return, you simply do the following command:

git checkout <file_path>

And presto, your file is back.

最丧也最甜 2024-07-29 07:34:56

git undelete path/to/file.ext

  1. 将其放入您的.bash_profile(或打开命令 shell 时加载的其他相关文件)中:

    git config --global alias.undelete '!sh -c "git checkout $(git rev-list -n 1 HEAD -- $1)^ -- $1 “-” 
      
  2. 然后使用:

    git 取消删除 path/to/file.ext 
      

此别名首先检查以查找该文件存在的最后一次提交,然后从该文件存在的最后一次提交中对该文件路径进行 Git 签出。 来源

git undelete path/to/file.ext

  1. Put this in your .bash_profile (or other relevant file that loads when you open a command shell):

    git config --global alias.undelete '!sh -c "git checkout $(git rev-list -n 1 HEAD -- $1)^ -- $1" -'
    
  2. Then use:

    git undelete path/to/file.ext
    

This alias first checks to find the last commit where this file existed, and then does a Git checkout of that file path from that last commit where this file existed. Source.

他是夢罘是命 2024-07-29 07:34:56

实际上,这个问题直接与 Git 有关,但是像我这样的人使用 GUI 工具,例如 WebStorm VCS 除了了解 Git CLI 命令之外。

我右键单击包含已删除文件的路径,然后转到 Git,然后单击“显示历史记录”。

输入图像描述这里

VCS 工具显示了所有修订版本,我可以看到每个版本的所有提交和更改。

输入图像描述这里

然后我选择我的朋友删除 PostAd.js 文件的提交。 现在请看下面:

“在此处输入图像描述”"

现在,我可以看到我想要删除的文件。 我只需双击文件名即可恢复。

输入图像描述这里

我知道我的答案不是 Git 命令,但它对于初学者和专业开发人员来说快速、可靠且简单。 WebStorm VCS 工具非常棒,非常适合与 Git 一起使用,并且不需要任何其他插件或工具。

Actually, this question is directly about Git, but somebody like me works with GUI tools like the WebStorm VCS other than knowing about Git CLI commands.

I right click on the path that contains the deleted file, and then go to Git and then click on Show History.

Enter image description here

The VCS tools show all revisions train and I can see all commits and changes of each of them.

Enter image description here

Then I select the commits that my friend delete the PostAd.js file. now see below:

Enter image description here

And now, I can see my desire deleted file. I just double-click on the filename and it recovers.

Enter image description here

I know my answer is not Git commands, but it is fast, reliable and easy for beginner and professional developers. WebStorm VCS tools are awesome and perfect for working with Git and it doesn't need any other plugin or tools.

故事未完 2024-07-29 07:34:56
git checkout /path/to/deleted.file
git checkout /path/to/deleted.file
拍不死你 2024-07-29 07:34:56

在许多情况下,将 coreutils(grep、sed 等)与 Git 结合使用会很有用。 我已经很了解这些工具,但对 Git 不太了解。 如果我想搜索已删除的文件,我会执行以下操作:

git log --raw | grep -B 30 

当我找到修订/提交时:

git checkout <rev>^ -- path/to/refound/deleted_file.c

就像其他人在我之前所说的那样。

该文件现在将恢复到删除之前的状态。 如果您想保留它,请记住将其重新提交到工作树。

D\t.*deleted_file.c'

当我找到修订/提交时:


就像其他人在我之前所说的那样。

该文件现在将恢复到删除之前的状态。 如果您想保留它,请记住将其重新提交到工作树。

In many cases, it can be useful to use coreutils (grep, sed, etc.) in conjunction with Git. I already know these tools quite well, but Git less so. If I wanted to do a search for a deleted file, I would do the following:

git log --raw | grep -B 30 

When I find the revision/commit:

git checkout <rev>^ -- path/to/refound/deleted_file.c

Just like others have stated before me.

The file will now be restored to the state it had before removal. Remember to re-commit it to the working tree if you want to keep it around.

D\t.*deleted_file.c'

When I find the revision/commit:


Just like others have stated before me.

The file will now be restored to the state it had before removal. Remember to re-commit it to the working tree if you want to keep it around.

燃情 2024-07-29 07:34:56

我也有同样的问题。 在不知不觉中,我创建了一个悬空提交

列出悬空提交

git fsck --lost-found

检查每个悬空提交

git reset --hard

当我移动到悬空提交时,我的文件重新出现。

git status 原因:

“HEAD 与 分离”

I had the same question. Without knowing it, I had created a dangling commit.

List dangling commits

git fsck --lost-found

Inspect each dangling commit

git reset --hard <commit id>

My files reappeared when I moved to the dangling commit.

git status for the reason:

“HEAD detached from <commit id where it detached>”

戏剧牡丹亭 2024-07-29 07:34:56

找到删除文件的提交:

git log --diff-filter=D --oneline -- path/to/file | cut -f -d ' '

示例输出:

4711174

从 Git 2.23 开始,实际上有一个 restore 命令。 它仍处于实验阶段,但为了恢复您在提交中删除的内容(本例中为 4711174),您可以键入:

git restore --source=4711174^ path/to/file

请注意我们想要的提交 ID 后面的 ^从删除文件的提交之前恢复某些内容。

--source 参数告诉 restore 命令在哪里查找要恢复的文件,它可以是任何提交,甚至是索引。

请参阅:git 2.23.0 的 git-restore 文档

Find the commit that deleted your file:

git log --diff-filter=D --oneline -- path/to/file | cut -f -d ' '

Sample output:

4711174

As of Git 2.23 there is actually a restore command. It is still experimental but in order to restore something you removed in a commit (4711174 in this case) you can then type:

git restore --source=4711174^ path/to/file

Note the ^ after the commit id as we want to restore something from the commit before the one that deleted the file.

The --source argument tells the restore command where to look for the file(s) to restore and it can be any commit and even the index.

See: git-restore doc for git 2.23.0

孤独陪着我 2024-07-29 07:34:56

我必须从特定提交中恢复一堆已删除的文件,并且我使用两个命令对其进行管理:

git show <rev> --diff-filter=D --summary --name-only --no-commit-id | xargs git checkout <rev>^ -- 
git show <rev> --diff-filter=D --summary --name-only --no-commit-id | xargs git reset HEAD 

(请注意每个命令末尾的尾随空格。)

文件已添加到 . gitignore 文件,然后使用 git rm 清除。 我需要恢复这些文件,然后取消暂存它们。 我有数百个文件需要恢复,并且像其他示例一样为每个文件手动键入内容会太慢。

I had to restore a bunch of deleted files from a specific commit, and I managed it with two commands:

git show <rev> --diff-filter=D --summary --name-only --no-commit-id | xargs git checkout <rev>^ -- 
git show <rev> --diff-filter=D --summary --name-only --no-commit-id | xargs git reset HEAD 

(Note the trailing space on the end of each command.)

The files had been added to the .gitignore file and then cleared with git rm. I needed to restore the files, but then unstage them. I had hundreds of files to restore, and typing things manually for each file as in the other examples was going to be far too slow.

凉城已无爱 2024-07-29 07:34:56
user@bsd:~/work/git$ rm slides.tex
user@bsd:~/work/git$ git pull 
Already up-to-date.
user@bsd:~/work/git$ ls slides.tex
ls: slides.tex: No such file or directory

恢复已删除的文件:

user@bsd:~/work/git$ git checkout
D       .slides.tex.swp
D       slides.tex
user@bsd:~/work/git$ git checkout slides.tex 
user@bsd:~/work/git$ ls slides.tex
slides.tex
user@bsd:~/work/git$ rm slides.tex
user@bsd:~/work/git$ git pull 
Already up-to-date.
user@bsd:~/work/git$ ls slides.tex
ls: slides.tex: No such file or directory

Restore the deleted file:

user@bsd:~/work/git$ git checkout
D       .slides.tex.swp
D       slides.tex
user@bsd:~/work/git$ git checkout slides.tex 
user@bsd:~/work/git$ ls slides.tex
slides.tex
遗忘曾经 2024-07-29 07:34:56

在我们的例子中,我们不小心删除了一次提交中的文件,后来我们意识到了一些提交中的错误,并希望取回所有被删除的文件,但不恢复那些被修改的文件。

根据查尔斯·贝利(Charles Bailey)的出色回答,这是我的俏皮话:

git co $(git rev-list -n 1 HEAD -- <file_path>)~1 -- $(git diff --name-status $(git rev-list -n 1 HEAD -- <file_path>)~1 head | grep '^D' | cut -f 2)

In our case we accidentally deleted files in a commit and some commits later we realized our mistake and wanted to get back all the files that were deleted, but not those that were modified.

Based on Charles Bailey's excellent answer, here is my one-liner:

git co $(git rev-list -n 1 HEAD -- <file_path>)~1 -- $(git diff --name-status $(git rev-list -n 1 HEAD -- <file_path>)~1 head | grep '^D' | cut -f 2)
过气美图社 2024-07-29 07:34:56

加分点:以下方法确实适用于文件/文件夹甚至从垃圾箱或回收站中删除的情况。

文件/文件夹从工作树中删除,但不删除尚未提交:

I. 如果您尚未对更改建立索引(git add),您可以恢复目录的内容:

git restore -- path/to/folder_OR_file

II。 如果删除已建立索引,则应首先重置:

git reset -- path/to/folder_OR_file

,然后执行,git Restore path/to/folder_OR_file

文件/文件夹是在过去的某些提交中删除:

  1. 使用git log --diff-filter=D --summary 获取删除文件/文件夹的提交的详细信息;
  2. 使用git checkout $commit~1 path/to/folder_OR_file 恢复已删除的文件/文件夹。
    其中$commit 是您在步骤 1 中找到的提交的 sha 值,例如 c7578994

Plus point: The below methods does work good for the scenario that files/folders got deleted even from your Trash or Recycle bin.

Files/Folders are deleted from working tree but not committed yet:

I. If you have not yet indexed (git add) your changes you can revert content of a directory:

git restore -- path/to/folder_OR_file

II. If the deletion is already indexed, you should reset that first:

git reset -- path/to/folder_OR_file

then perform, git restore path/to/folder_OR_file

Files/Folders are deleted in some commit in the past:

  1. Use git log --diff-filter=D --summary to get details of the commits in which files/folder were deleted;
  2. Use git checkout $commit~1 path/to/folder_OR_file to restore the deleted files/folder.
    Where $commit is the sha-value of the commit you've found at step 1, e.g. c7578994
乖不如嘢 2024-07-29 07:34:56

如果您知道删除文件的提交,请运行此命令,其中 是删除文件的提交:

git diff --diff-filter=D --name-only <SHA1_deletion>~1 <SHA1_deletion> | xargs git checkout <SHA1_deletion>~1 --

管道之前的部分列出了在犯罪; 它们都是从之前的提交中检出以恢复它们的。

If you know the commit that deleted the file(s), run this command where <SHA1_deletion> is the commit that deleted the file:

git diff --diff-filter=D --name-only <SHA1_deletion>~1 <SHA1_deletion> | xargs git checkout <SHA1_deletion>~1 --

The part before the pipe lists all the files that were deleted in the commit; they are all checkout from the previous commit to restore them.

獨角戲 2024-07-29 07:34:56

要找到最好的方法,请尝试一下。


首先,找到删除文件的提交的提交 ID。
它将为您提供删除文件的提交摘要。

git log --diff-filter=D --summary

git checkout 84sdhfddbdddf~1

注意:84sdhfddbddd 是您的commit id

通过这个您可以轻松恢复所有已删除的文件。

For the best way to do that, try it.


First, find the commit id of the commit that deleted your file.
It will give you a summary of commits which deleted files.

git log --diff-filter=D --summary

git checkout 84sdhfddbdddf~1

Note: 84sdhfddbddd is your commit id

Through this you can easily recover all deleted files.

百变从容 2024-07-29 07:34:56

简单而精确 -

首先,获取一个最新的稳定提交,其中包含该文件 -

git log 

假设您找到 $commitid 1234567...,然后

git checkout <$commitid> $fileName

这将恢复该提交中的文件版本。

Simple and precise-

First of all, get a latest stable commit in which you have that file by -

git log 

Say you find $commitid 1234567..., then

git checkout <$commitid> $fileName

This will restore the file version which was in that commit.

晨与橙与城 2024-07-29 07:34:56

如果您只想恢复 master 中的相同文件,这是我发现的最简单的解决方案:

git checkout master -- path/to/File.java

This is the easiest solution I found if you want just restore the same file in master:

git checkout master -- path/to/File.java
〃温暖了心ぐ 2024-07-29 07:34:56

您始终可以 git revert 删除文件的提交。 (这假设删除是提交中的唯一更改。

> git log
commit 2994bda49cd97ce49099953fc3f76f7d3c35d1d3
Author: Dave <[email protected]>
Date:   Thu May 9 11:11:06 2019 -0700

    deleted readme.md

如果您继续工作,并且后来意识到您不想提交该删除提交,则可以使用以下命令恢复它: :

> git revert 2994bd

现在git log显示:

> git log
Author: Dave <[email protected]>
Date:   Thu May 9 11:17:41 2019 -0700

    Revert "deleted readme"

    This reverts commit 2994bda49cd97ce49099953fc3f76f7d3c35d1d3.

并且readme.md已恢复到存储库中。

You could always git revert your commit which deleted the file. (This assumes that the deletion was the only change in the commit.)

> git log
commit 2994bda49cd97ce49099953fc3f76f7d3c35d1d3
Author: Dave <[email protected]>
Date:   Thu May 9 11:11:06 2019 -0700

    deleted readme.md

And if you've continued work, and realized later that you didn't want to commit that deletion commit, you could revert it using:

> git revert 2994bd

Now git log shows:

> git log
Author: Dave <[email protected]>
Date:   Thu May 9 11:17:41 2019 -0700

    Revert "deleted readme"

    This reverts commit 2994bda49cd97ce49099953fc3f76f7d3c35d1d3.

And readme.md has been restored into the repository.

情绪少女 2024-07-29 07:34:56

如果删除尚未提交,下面的命令将在工作树中恢复已删除的文件。

$ git checkout -- <file>

您可以使用以下命令获取工作树中所有已删除文件的列表。

$ git ls-files --deleted

如果删除已提交,请找到发生删除的提交,然后从该提交恢复文件。

$ git rev-list -n 1 HEAD -- <file>
$ git checkout <commit>^ -- <file>

如果您正在查找要恢复的文件的路径,以下命令将显示所有已删除文件的摘要。

$ git log --diff-filter=D --summary

If the deletion has not been committed, the command below will restore the deleted file in the working tree.

$ git checkout -- <file>

You can get a list of all the deleted files in the working tree using the command below.

$ git ls-files --deleted

If the deletion has been committed, find the commit where it happened, then recover the file from this commit.

$ git rev-list -n 1 HEAD -- <file>
$ git checkout <commit>^ -- <file>

In case you are looking for the path of the file to recover, the following command will display a summary of all deleted files.

$ git log --diff-filter=D --summary
ˇ宁静的妩媚 2024-07-29 07:34:56

我也有这个问题,使用下面的代码将以前的文件检索到本地目录:

git checkout <file path with name>

下面的示例对我有用:

git checkout resources/views/usaSchools.blade.php

I also have this problem using the below code to retrieve a previous file to a local directory:

git checkout <file path with name>

The below example is working for me:

git checkout resources/views/usaSchools.blade.php

浊酒尽余欢 2024-07-29 07:34:56

您可以查看已删除的文件:

git checkout

输出

D       index.html

要恢复它:

git restore index.html

如果您删除了多个文件并且需要恢复所有使用:

git restore .

请参阅 Gif 图片
恢复文件通过 git

You can checkout deleted files:

git checkout

Output

D       index.html

To restore it:

git restore index.html

If you deleted multi files and you need to restore all use:

git restore .

See Gif image
restore files by git

酷遇一生 2024-07-29 07:34:56
$ git log --diff-filter=D --summary  | grep "delete" | sort
$ git log --diff-filter=D --summary  | grep "delete" | sort
萤火眠眠 2024-07-29 07:34:56

为了使用 Git 恢复所有已删除的文件,您还可以执行以下操作:

git checkout $(git ls-files --deleted)

其中 git ls-files --deleted 列出所有已删除的文件,并且 git checkout $(git command) 恢复参数中的文件列表。

In order to restore all deleted file with Git, you can also do:

git checkout $(git ls-files --deleted)

Where git ls-files --deleted lists all deleted files and git checkout $(git command) restores the list of files in a parameter.

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