tags:

views:

1350

answers:

3

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?

+1  A: 
@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$
Banengusk
+2  A: 

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
You also may git checkout <commit hash> without creating the branch.
Banengusk
+5  A: 

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