9

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?

Community
  • 1
  • 1
Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697

3 Answers3

11

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

Jakub Narębski
  • 309,089
  • 65
  • 217
  • 230
3

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.

kinghajj
  • 253
  • 1
  • 8
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$ 
Daniel Fanjul
  • 3,493
  • 3
  • 26
  • 29