无法恢复 Git 中的文件

发布于 2024-07-14 16:42:48 字数 563 浏览 6 评论 0原文

可能的重复:
恢复 Git 存储库中已删除的文件

我有两个分支在我的 Git 中,master 和 newFeature。 在分支 newFeature 中,我首先在终端中物理删除了 fileA,然后在 Git 中通过

git rm fileA

随后运行,

git add .
git commit

现在,我再次需要 fileA。 我的想法是,只需切换到分支 master 即可将其恢复。 我显然错了,因为我找不到fileA

如何使用 Git 取回 fileA?

Possible Duplicate:
Restore a deleted file in a Git repo

I have two branches in my Git, master and newFeature.
At the branch newFeature, I removed the fileA physically first in terminal and then in Git by

git rm fileA

Subsequently, I run

git add .
git commit

Right now, I need the fileA again.
I had the idea that I can get it back, by simply switching to the branch master.
I was apparently wrong, since I cannot find the fileA.

How can I get the fileA back with Git?

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

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

发布评论

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

评论(3

樱娆 2024-07-21 16:42:48

首先,您需要找到 fileA 的最新版本。 您可以使用“git log -p”或“git Whatchanged”来检查它何时被删除,或者您可以使用“git ls-files revision> -- fileA”来检查文件是否被删除存在于给定提交中,其中“<修订>” 可以是masternewFeature^newFeature^表示newFeature的父级)。

然后你需要检查它,使用

$ git checkout <revision> -- fileA

或重定向“git show”输出

$ git show <revision>:fileA > fileA

不要忘记将文件添加到git(如果需要)!

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)!

巨坚强 2024-07-21 16:42:48

在删除 fileA 之前在提交处创建一个标记或分支,将其签出,将 fileA 复制到其他位置,然后再次签出 newFeature 分支。 其余的应该很简单。

Create a tag or branch at the commit before you deleted fileA, check it out, copy fileA somewhere else, then checkout the newFeature branch again. The rest should be pretty simple.

∞琼窗梦回ˉ 2024-07-21 16:42:48
@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$ 
@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$ 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文