4

I have a bad feeling my work is lost. Is there a way to possibly get them back?

Here's a gist of what I did:

# Initially
$ git init -q
$ echo foo >foo
$ git add foo
$ git commit -m initial
[master (root-commit) 399ac4f] initial
 1 file changed, 1 insertion(+)
 create mode 100644 foo

# Work work work
$ rm foo
$ mkdir foo && echo bar >foo/bar

# This is where things went bad
$ git stash
Saved working directory and index state WIP on master: 399ac4f initial
HEAD is now at 399ac4f initial
$ git stash pop
Removing foo
# On branch master
# ...
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (c8353a2223b73ceb9aa73fac64829919b68c86e9)

# What happened to my work!?
$ find foo
find: foo: No such file or directory

$ git --version
git version 1.7.11.3

Unfortulately, I don't think foo/bar ever saw the repository/index, as I hadn't run any git commands on it prior to this.

Alexis King
  • 43,109
  • 15
  • 131
  • 205
antak
  • 19,481
  • 9
  • 72
  • 80

2 Answers2

4

You can easily recover the lost stash (you can see its revision on the screen). But it is no use.

git stash does not save new files (foo/ directory content in your case), but it does git reset --hard before exit. And that restores the working directory which effectively removes foo/.

I suggest using some kind of deleted files recovering tool.

P.S. Sent an email about this into git mailing list. They said it is a bug, because git stash is intended to be a safe command. But for now just use git stash -u. This way git saves to the stash all (even untracked) files

antak
  • 19,481
  • 9
  • 72
  • 80
Oleksandr Pryimak
  • 1,561
  • 9
  • 11
  • *using some kind of deleted files recovering tool* seems like to most realistic option. Thanks for sending the bug report. Cheers. – antak Jul 31 '12 at 00:57
  • Just discovered this behavior the hard way with git 2.2.1. Any idea what this bug's current status is? I was able to find some mentions on the mailing list ([1](http://article.gmane.org/gmane.comp.version-control.git/202332/match=11680453), [2](http://article.gmane.org/gmane.comp.version-control.git/205540/match=git+stash+data+loss), [3](http://article.gmane.org/gmane.comp.version-control.git/229703/match=git+stash+data+loss)), but not how it was resolved. (Does the project have any sort of bug tracker besides the mailing list?) – doctaphred Mar 12 '15 at 21:34
0

Im a "Git stash \ Git stash apply"-man myself, but this question and answer should probably help.

How to recover a dropped stash in Git?

Community
  • 1
  • 1
Cleric
  • 3,167
  • 3
  • 23
  • 24