假设我在 Git 存储库中。 我删除一个文件并提交该更改。 我继续工作并做出更多承诺。 然后,我发现删除该文件后需要恢复该文件。
我知道我可以使用 git checkout 签出文件 -- filename.txt,但我不知道该文件何时被删除。
- 如何找到删除给定文件名的提交?
- 如何将该文件恢复到我的工作副本中?
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.
- How do I find the commit that deleted a given filename?
- How do I restore that file back into my working copy?
发布评论
评论(30)
查找影响给定路径的最后一次提交。 由于该文件不在 HEAD 提交中,因此之前的提交一定已将其删除。
然后使用脱字号 (
^
) 符号检查之前提交时的版本:或者在一个命令中,如果
$file
是有问题的文件。如果您使用 zsh 并启用了 EXTENDED_GLOB 选项,则插入符号将不起作用。 您可以使用
~1
代替。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.
Then checkout the version at the commit before, using the caret (
^
) symbol:Or in one command, if
$file
is the file in question.If you are using zsh and have the EXTENDED_GLOB option enabled, the caret symbol won't work. You can use
~1
instead.获取所有已删除文件的提交,以及已删除的文件:
记下所需的提交哈希,例如
e4e6d4d5e5c59c69f3bd7be2
。将已删除的文件从之前的一次提交 (
~1
) 恢复到上面确定的提交 (e4e6d4d5e5c59c69f3bd7be2
):注意
~1
。 波形符规范将为您提供指定提交的第 n 个祖父母。Get all the commits which have deleted files, as well as the files that were deleted:
Make note of the desired commit hash, e.g.
e4e6d4d5e5c59c69f3bd7be2
.Restore the deleted file from one commit prior (
~1
) to the commit that was determined above (e4e6d4d5e5c59c69f3bd7be2
):Note the
~1
. The tilde spec will give you the nth grandparent of the named commit.要恢复文件夹中所有已删除的文件:
To restore all deleted files in a folder:
如果您删除了最新
HEAD
提交中存在的文件,您可以使用以下命令恢复它:If you deleted a file that exists in the latest
HEAD
commit, you can restore it using:如果你疯了,请使用
git-一分为二
。 要做的事情如下:现在是运行自动化测试的时候了。 如果
foo.bar
存在,shell 命令'[ -e foo.bar ]'
将返回 0,否则返回 1。 git-bisect 的“run”命令将使用二分搜索自动查找测试失败的第一个提交。 它从给定范围的中间(从好到坏)开始,并根据指定测试的结果将其减半。现在您位于删除它的提交处。 从这里,您可以跳回到未来并使用
git-revert
撤消更改,或者您可以返回一次提交并手动检查损坏情况:
If you’re insane, use
git-bisect
. Here's what to do:Now it's time to run the automated test. The shell command
'[ -e foo.bar ]'
will return 0 iffoo.bar
exists, and 1 otherwise. The "run" command ofgit-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.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,or you could go back one commit and manually inspect the damage:
我最喜欢的新别名,基于 bonyiii 的 答案(已投票),以及我自己关于“将参数传递给 Git 别名命令":
我丢失了一个文件,在几次提交前被错误删除了?
快:
危机避免。
警告,Git 2.23(2019 年第 3 季度)推出了名为 实验命令 .com/docs/git-restore" rel="nofollow noreferrer">
git 恢复
(!)。因此重命名这个别名(如下所示)。
罗伯特·戴利提出在评论中以下别名:
和 jegan 添加 在评论中:
My new favorite alias, based on bonyiii's answer (upvoted), and my own answer about "Pass an argument to a Git alias command":
I have lost a file, deleted by mistake a few commits ago?
Quick:
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:
And jegan adds in the comments:
如果您知道文件名,这是使用基本命令的简单方法:
列出该文件的所有提交。
最后一次提交(最顶层)是删除该文件的提交。 所以你需要恢复倒数第二个提交。
If you know the filename, this is an easy way with basic commands:
List all the commits for that file.
The last commit (topmost) is the one that deleted the file. So you need to restore the second to last commit.
恢复已删除并提交的文件:
已在 Git 版本 1.7.5.4 上进行测试。
To restore a deleted and commited file:
It was tested on Git version 1.7.5.4.
我有这个解决方案。
使用以下方法之一获取删除文件的提交 ID。
git log --grep=*word*
git log -Sword
git 日志 | grep --context=5 *单词*
记住任何事情
你应该得到类似的东西:
3。 现在使用提交 ID bfe68bd117e1091c96d2976c99b3bcc8310bebe7 执行以下操作:
由于提交 ID 引用文件已被删除的提交,因此您需要引用 bfe68b 之前的提交,您可以通过附加
^1
来完成。 这意味着:给我 bfe68b 之前的提交。I've got this solution.
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 hardlyremember anything
You should get something like:
3. Now using the commit id bfe68bd117e1091c96d2976c99b3bcc8310bebe7 do:
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.如果您只进行了更改并删除了文件,但没有提交它,现在您放弃了更改,
但删除的文件没有返回,您只需执行以下命令:
很快,您的文件就回来了。
If you only made changes and deleted a file, but not commit it, and now you broke up with your changes
but your deleted files did not return, you simply do the following command:
And presto, your file is back.
git undelete path/to/file.ext
将其放入您的
.bash_profile
(或打开命令 shell 时加载的其他相关文件)中:然后使用:
此别名首先检查以查找该文件存在的最后一次提交,然后从该文件存在的最后一次提交中对该文件路径进行 Git 签出。 来源。
git undelete path/to/file.ext
Put this in your
.bash_profile
(or other relevant file that loads when you open a command shell):Then use:
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.
实际上,这个问题直接与 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.
The VCS tools show all revisions train and I can see all commits and changes of each of them.
Then I select the commits that my friend delete the
PostAd.js
file. now see below:And now, I can see my desire deleted file. I just double-click on the filename and it recovers.
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.
在许多情况下,将 coreutils(grep、sed 等)与 Git 结合使用会很有用。 我已经很了解这些工具,但对 Git 不太了解。 如果我想搜索已删除的文件,我会执行以下操作:
当我找到修订/提交时:
就像其他人在我之前所说的那样。
该文件现在将恢复到删除之前的状态。 如果您想保留它,请记住将其重新提交到工作树。
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:
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.
我也有同样的问题。 在不知不觉中,我创建了一个悬空提交。
列出悬空提交
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>”
找到删除文件的提交:
示例输出:
从 Git 2.23 开始,实际上有一个
restore
命令。 它仍处于实验阶段,但为了恢复您在提交中删除的内容(本例中为 4711174),您可以键入:请注意我们想要的提交 ID 后面的 ^从删除文件的提交之前恢复某些内容。
--source
参数告诉restore
命令在哪里查找要恢复的文件,它可以是任何提交,甚至是索引。请参阅:git 2.23.0 的 git-restore 文档
Find the commit that deleted your file:
Sample output:
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: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 therestore
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
我必须从特定提交中恢复一堆已删除的文件,并且我使用两个命令对其进行管理:
(请注意每个命令末尾的尾随空格。)
文件已添加到 . gitignore 文件,然后使用 git rm 清除。 我需要恢复这些文件,然后取消暂存它们。 我有数百个文件需要恢复,并且像其他示例一样为每个文件手动键入内容会太慢。
I had to restore a bunch of deleted files from a specific commit, and I managed it with two commands:
(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.恢复已删除的文件:
Restore the deleted file:
在我们的例子中,我们不小心删除了一次提交中的文件,后来我们意识到了一些提交中的错误,并希望取回所有被删除的文件,但不恢复那些被修改的文件。
根据查尔斯·贝利(Charles Bailey)的出色回答,这是我的俏皮话:
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:
加分点:以下方法确实适用于文件/文件夹甚至从垃圾箱或回收站中删除的情况。
文件/文件夹从工作树中删除,但不删除尚未提交:
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
文件/文件夹是在过去的某些提交中删除:
git log --diff-filter=D --summary
获取删除文件/文件夹的提交的详细信息;git checkout $commit~1 path/to/folder_OR_file
恢复已删除的文件/文件夹。其中
$commit
是您在步骤 1 中找到的提交的 sha 值,例如 c7578994Plus 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:
git log --diff-filter=D --summary
to get details of the commits in which files/folder were deleted;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如果您知道删除文件的提交,请运行此命令,其中
是删除文件的提交:管道之前的部分列出了在犯罪; 它们都是从之前的提交中检出以恢复它们的。
If you know the commit that deleted the file(s), run this command where
<SHA1_deletion>
is the commit that deleted the file: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.
要找到最好的方法,请尝试一下。
首先,找到删除文件的提交的提交 ID。
它将为您提供删除文件的提交摘要。
注意:
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.
Note:
84sdhfddbddd
is yourcommit id
Through this you can easily recover all deleted files.
简单而精确 -
首先,获取一个最新的稳定提交,其中包含该文件 -
假设您找到 $commitid 1234567...,然后
这将恢复该提交中的文件版本。
Simple and precise-
First of all, get a latest stable commit in which you have that file by -
Say you find $commitid 1234567..., then
This will restore the file version which was in that commit.
如果您只想恢复 master 中的相同文件,这是我发现的最简单的解决方案:
This is the easiest solution I found if you want just restore the same file in master:
您始终可以
git revert
删除文件的提交。 (这假设删除是提交中的唯一更改。)如果您继续工作,并且后来意识到您不想提交该删除提交,则可以使用以下命令恢复它: :
现在
git log
显示:并且
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.)And if you've continued work, and realized later that you didn't want to commit that deletion commit, you could revert it using:
Now
git log
shows:And
readme.md
has been restored into the repository.如果删除尚未提交,下面的命令将在工作树中恢复已删除的文件。
您可以使用以下命令获取工作树中所有已删除文件的列表。
如果删除已提交,请找到发生删除的提交,然后从该提交恢复文件。
如果您正在查找要恢复的文件的路径,以下命令将显示所有已删除文件的摘要。
If the deletion has not been committed, the command below will restore the deleted file in the working tree.
You can get a list of all the deleted files in the working tree using the command below.
If the deletion has been committed, find the commit where it happened, then recover the file from this commit.
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 checkout resources/views/usaSchools.blade.php
I also have this problem using the below code to retrieve a previous file to a local directory:
The below example is working for me:
git checkout resources/views/usaSchools.blade.php
您可以查看已删除的文件:
输出
要恢复它:
如果您删除了多个文件并且需要恢复所有使用:
请参阅 Gif 图片
You can checkout deleted files:
Output
To restore it:
If you deleted multi files and you need to restore all use:
See Gif image
为了使用 Git 恢复所有已删除的文件,您还可以执行以下操作:
其中 git ls-files --deleted 列出所有已删除的文件,并且 git checkout $(git command) 恢复参数中的文件列表。
In order to restore all deleted file with Git, you can also do:
Where
git ls-files --deleted
lists all deleted files andgit checkout $(git command)
restores the list of files in a parameter.