On Fri, 29 Jul 2016 16:09:36 -0700 (PDT)
GUGLHUPF <ralfschiff...@tropo.com> wrote:

> Hi,
> fairly new to git. Today I did a "git add somefile" and then decided
> I wanted to unstage it. I did then a "git rm -f somefile". There was
> no git command in between. Particularly no commit.
> git wiped the file from disk. I worked very hard on that file
> (several days( and I really hope this can be recovered. I could not
> find a solution on the web. 
> For completeness I did a git reset HEAD somefile because that's what
> I found on the web, but it didn't recover the file. 
> Is there a way to recver somefile?

Yes, there are pretty good chances it's still possible.
The idea is that Git never immediately removes stuff from the
repository -- to speed things up.  Instead, whatever is deleted becomes
"garbage" which is reclaimed from time to time either by hand or by Git
itself running the `git gc --auto` under certain conditions when its
heuristics tell it there's too much garbage in the repo.

So since you `git add`-ed your lost contents, Git at that time made a
so-called "blob" of it, placed it into the repository and put a
reference to it in the index.  When you deleted that entry, the blob
pointed to by it became "dangling" (i.e. not referenced by anything),
and hence garbage.

OK, so back to recovery.  The command which -- among other things -- is
able to find dangling objects is `git fsck` ("fsck" is a play on the
name of a traditional Unix program for checking file systems for errors:
"file system check").  You need to run

  git fsck --lost-found

and then look under the ".git/lost-found/other" directory this program
created for you and populated with whatever dangling objects it was
found.  Review the contents of each file created there, and once you
locate your lost file, simply copy it over back to the work tree under
its original name.

Once satisfied, delete that ".git/lost-found" directory.

Here's a sample session:

  $ git init foo
  Initialized empty Git repository in C:/Users/kostix/foo/.git/
  $ cd foo
  $ echo test >aaa.txt
  $ git add aaa.txt
  $ git commit
  [master (root-commit) 8309e63] First commit
   1 file changed, 1 insertion(+)
   create mode 100644 aaa.txt
  $ echo tada >aaa.txt
  $ git add aaa.txt
  $ git commit
  [master 80a3924] Second commit
   1 file changed, 1 insertion(+), 1 deletion(-)
  $ echo xyzzy >aaa.txt
  $ git add aaa.txt
  $ git rm -f aaa.txt
  rm 'aaa.txt'
  $ git fsck --lost-found
  Checking object directories: 100% (256/256), done.
  dangling blob f34893577adbcf81564ef9cb53d4fc6bf95e0043
  $ ls -1 .git/lost-found/other
  $ cat .git/lost-found/other/f34893577adbcf81564ef9cb53d4fc6bf95e0043
  $ copy .git/lost-found/other/f34893577adbcf81564ef9cb53d4fc6bf95e0043 aaa.txt

  $ cat aaa.txt

Hope this helps!

You received this message because you are subscribed to the Google Groups "Git 
for human beings" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to git-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to