假设我在 Git 存储库中。我删除一个文件并提交该更改。我继续工作并做出更多的承诺。然后,我发现我需要恢复该文件。
我知道我可以使用 签出文件git checkout HEAD^ foo.bar
,但我真的不知道该文件何时被删除。
- 找到删除给定文件名的提交的最快方法是什么?
- 将该文件恢复到我的工作副本中的最简单方法是什么?
我希望我不必手动浏览我的日志,检查给定 SHA 的整个项目,然后手动将该文件复制到我的原始项目检查中。
假设我在 Git 存储库中。我删除一个文件并提交该更改。我继续工作并做出更多的承诺。然后,我发现我需要恢复该文件。
我知道我可以使用 签出文件git checkout HEAD^ foo.bar
,但我真的不知道该文件何时被删除。
我希望我不必手动浏览我的日志,检查给定 SHA 的整个项目,然后手动将该文件复制到我的原始项目检查中。
查找影响给定路径的最后一个提交。由于该文件不在 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"
git log --diff-filter=D --summary
获取所有已删除文件和已删除文件的提交;git checkout $commit~1 path/to/file.ext
恢复已删除的文件。$commit
您在第 1 步中找到的提交的价值在哪里,例如e4cf499627
要恢复文件夹中所有已删除的文件,请输入以下命令。
git ls-files -d | xargs git checkout --
我来这个问题是为了恢复我刚刚删除但尚未提交更改的文件。万一您发现自己处于这种情况,您需要做的就是以下几点:
git checkout HEAD -- path/to/file.ext
如果您疯了,请使用git-bisect
. 这是做什么:
git bisect start
git bisect bad
git bisect good <some commit where you know the file existed>
现在是时候运行自动化测试了。如果存在,shell 命令'[ -e foo.bar ]'
将返回 0 ,否则返回 1。foo.bar
的“运行”命令git-bisect
将使用二进制搜索自动找到测试失败的第一个提交。它从给定范围(从好到坏)的中间开始,并根据指定测试的结果将其减半。
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 .
我最喜欢的新别名,基于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 年第三季度)的推出,实验性命令名为git restore
(!)。
所以重命名这个别名(如下图)。
Robert Dailey在评论中提出以下别名:
restore-file = !git checkout $(git rev-list -n 1 HEAD -- "$1")^ -- "$1"
为了从命令行设置别名,我使用了这个命令:
git config --global alias.restore "\!git checkout \$(git rev-list -n 1 HEAD -- \"\$1\")^ -- \"\$1\""
如果您知道文件名,这是使用基本命令的简单方法:
列出该文件的所有提交。
git log -- path/to/file
最后一次提交(最顶层)是删除文件的那个。所以你需要恢复倒数第二个提交。
git checkout {second to last commit} -- path/to/file
要恢复已删除和提交的文件:
git reset HEAD some/path
git checkout -- some/path
它在 Git 版本 1.7.5.4 上进行了测试。
我有这个解决方案。
使用以下方法之一获取文件被删除的提交的 id。
git log --grep=*word*
git log -Sword
git log | grep --context=5 *word*
git log --stat | grep --context=5 *word*
# 如果您几乎不记得任何内容,建议您使用你应该得到类似的东西:
提交 bfe68bd117e1091c96d2976c99b3bcc8310bebe7 作者:Alexander Orlov 日期:2011 年 5 月 12 日星期四 23:44:27 +0200
replaced deprecated GWT class - gwtI18nKeySync.sh, an outdated (?, replaced by a Maven goal) I18n generation script
提交 3ea4e3af253ac6fd1691ff6bb89c964f54802302 作者:Alexander Orlov 日期:2011 年 5 月 12 日星期四 22:10:22 +0200
3 . 现在使用提交 ID bfe68bd117e1091c96d2976c99b3bcc8310bebe7 执行以下操作:
git checkout bfe68bd117e1091c96d2976c99b3bcc8310bebe7^1 yourDeletedFile.java
由于提交 id 引用了文件已被删除的提交,您需要在 bfe68b 之前引用提交,您可以通过附加^1
. 这意味着:在 bfe68b 之前给我提交。
如果您只进行了更改并删除了一个文件,但没有提交它,现在您与您的更改分手了
git checkout -- .
但是您删除的文件没有返回,您只需执行以下命令:
git checkout <file_path>
很快,你的文件又回来了。
git checkout /path/to/deleted.file
git undelete path/to/file.ext
将其放入您的.bash_profile
(或打开命令外壳时加载的其他相关文件):
git config --global alias.undelete '!sh -c "git checkout $(git rev-list -n 1 HEAD -- $1)^ -- $1" -'
然后使用:
git undelete path/to/file.ext
此别名首先检查以查找该文件存在的最后一次提交,然后从该文件存在的最后一次提交中对该文件路径进行 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 不太了解。如果我想搜索已删除的文件,我会执行以下操作:
git log --raw | grep -B 30 $'D\t.*deleted_file.c'
当我找到修订/提交时:
git checkout <rev>^ -- path/to/refound/deleted_file.c
就像其他人在我之前所说的那样。
该文件现在将恢复到删除之前的状态。如果您想保留它,请记住将其重新提交到工作树。
我必须从特定提交中恢复一堆已删除的文件,并使用两个命令对其进行管理:
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 文件中,然后使用 .gitignore 清除git rm
。我需要恢复文件,然后取消暂存它们。我有数百个文件要恢复,并且像在其他示例中那样为每个文件手动输入内容会太慢。
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.
在我们的例子中,我们不小心删除了提交中的文件,后来我们意识到我们的错误,并希望取回所有被删除的文件,而不是那些被修改的文件。
根据查尔斯·贝利的出色回答,这是我的单线:
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)
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
如果您知道删除文件的提交,请运行以下命令<SHA1_deletion>
,删除文件的提交在哪里:
git diff --diff-filter=D --name-only <SHA1_deletion>~1 <SHA1_deletion> | xargs git checkout <SHA1_deletion>~1 --
管道前面的部分列出了提交中删除的所有文件;它们都是从以前的提交中签出以恢复它们。
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.
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.
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 <dave@domain.com>
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 <dave@domain.com>
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.
Plus point: The below methods does work good for the scenario that files/folders got deleted even from your Trash or Recycle bin.
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
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. c7578994I 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
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
This is the easiest solution I found if you want just restore the same file in master:
git checkout master -- path/to/File.java
$ git log --diff-filter=D --summary | grep "delete" | sort
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.