可能重复:
在 Git 存储库中恢复已删除的文件
我的 Git 中有两个分支,master 和 newFeature。在分支newFeature,我首先在终端物理上删除了 fileA ,然后在 Git 中删除了
git rm fileA
随后,我跑
git add .
git commit
现在,我再次需要fileA。我有一个想法,我可以通过简单地切换到分支master来取回它。我显然错了,因为我找不到fileA。
如何使用 Git 取回 fileA?
可能重复:
在 Git 存储库中恢复已删除的文件
我的 Git 中有两个分支,master 和 newFeature。在分支newFeature,我首先在终端物理上删除了 fileA ,然后在 Git 中删除了
git rm fileA
随后,我跑
git add .
git commit
现在,我再次需要fileA。我有一个想法,我可以通过简单地切换到分支master来取回它。我显然错了,因为我找不到fileA。
如何使用 Git 取回 fileA?
First, you need to find where you have last version of fileA
. You can use "git log -p" or "git whatchanged" to check when it was deleted, or you can use "git ls-files <revision> -- fileA" to check if file is present in given commit, where '<revision>' can be master or newFeature^ (newFeature^ means parent of newFeature).
Then you need to check it out, either using
$ git checkout <revision> -- fileA
or redirect "git show" output
$ git show <revision>:fileA > fileA
Don't forget to add file to git (if needed)!
在删除 fileA 之前在提交处创建标签或分支,将其签出,将 fileA 复制到其他地方,然后newFeature
再次签出分支。其余的应该很简单。
@titan:~$ cd /tmp/
@titan:/tmp$ mkdir x
@titan:/tmp$ git init
Initialized empty Git repository in /tmp/.git/
@titan:/tmp$ echo a > a
@titan:/tmp$ git add a
@titan:/tmp$ git ci -m a
Created initial commit c835beb: a
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 a
@titan:/tmp$ git rm a
rm 'a'
@titan:/tmp$ git ci -m b
Created commit de97fae: b
1 files changed, 0 insertions(+), 1 deletions(-)
delete mode 100644 a
@titan:/tmp$ git whatchanged
commit de97fae7a72375ffa192643836ec8273ff6f762b
Date: Wed Mar 11 17:35:57 2009 +0100
b
:100644 000000 7898192... 0000000... D a
commit c835beb7c0401ec27d00621dcdafd366d2cfdcbe
Date: Wed Mar 11 17:35:51 2009 +0100
a
:000000 100644 0000000... 7898192... A a
@titan:/tmp$ git show 7898192
a
@titan:/tmp$ git show 7898192 > a
@titan:/tmp$