8

重複の可能性:
Git リポジトリで削除されたファイルを復元する

Git にはmaster と newFeatureの 2 つのブランチがあります。ブランチnewFeatureで、fileA を最初にターミナルで物理的に削除し、次に Git で

git rm fileA

その後、私は実行します

git add .
git commit

今、もう一度fileAが必要です。ブランチマスターに切り替えるだけで、それを取り戻すことができるという考えがありました。fileAが見つからないため、明らかに間違っていました。

GitでfileAを取り戻すにはどうすればよいですか?

4

3 に答える 3

11

まず、最新バージョンの がある場所を見つける必要がありますfileA。「git log -p」または「git whatchanged」を使用して、いつ削除されたかを確認できます。または、「git ls-files < Revision > -- fileA」を使用して、ファイルが特定のコミットに存在するかどうかを確認できます。リビジョン>' はmasterまたはnewFeature^ ( newFeature^はnewFeatureの親を意味します) です。

次に、次のいずれかを使用して、それをチェックアウトする必要があります

$ git checkout <revision> -- fileA

または「git show」出力をリダイレクトします

$ git show <revision>:fileA > fileA

ファイルを git に追加することを忘れないでください (必要な場合)。

于 2009-03-11T19:21:12.633 に答える
3

fileAを削除する前に、コミット時にタグまたはブランチを作成し、チェックアウトして、fileAを別の場所にコピーしてから、newFeatureブランチを再度チェックアウトします。残りはかなり単純なはずです。

于 2009-03-11T17:26:41.490 に答える
1
@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$ 
于 2009-03-11T16:38:05.600 に答える