1

I don't recall how my git local repo got into this state, but here is the situation:

My status on the command line says "nothing to commit, working tree clean".

git stash show -p stash@{0} shows a diff of some changes, and as I casually scroll through (not really understanding how the language of git works), I see the specific words that I remember writing within certain functions, so I feel encouraged because I know that my work isn't lost.

But when I run git stash apply stash@{0} I see:

CONFLICT (rename/delete): datastore/static/js/app/main.js deleted in Stashed changes and renamed to assets/js/app/helpers/main.js in Updated upstream. Version Updated upstream of assets/js/app/helpers/main.js left in tree.
CONFLICT (rename/delete): datastore/static/js/app/filters.js deleted in Stashed changes and renamed to assets/js/app/helpers/filters.js in Updated upstream. Version Updated upstream of assets/js/app/helpers/filters.js left in tree.

And git status shows:

Unmerged paths:
  (use "git restore --staged <file>..." to unstage)
  (use "git add <file>..." to mark resolution)
        added by us:     assets/js/app/helpers/filters.js
        added by us:     assets/js/app/helpers/main.js

I tried running git add ., but then my files didn't contain the changes that existed in the stash.

So then I reset the situation (got back to "nothing to commit, working tree clean"), ran git stash apply stash@{0}, and then this time ran git restore --staged assets/js/app/helpers/*, and the result doesn't seem any different to me (compared to when I ran git add .).

In both cases, when I inspect my files, certain key words are missing that exist in the stash.

How can I recover my work?

Ryan
  • 22,332
  • 31
  • 176
  • 357
  • 1
    You might want to use `git reflog` for inspecting the recent changes. If you find a state which you know to be clean, you can `git reset --hard` to the hash found. For the stashes: if you `git stash apply`, the applied stash remains in the list. If you apply it a second time, then conflicts might happen. Check if you still need all those stashes. I ususally use `git stash pop` to apply and immediately remove a stash. Just in case... you might want to make a backup of the current state by creating a branch (but not switching to it). – Adrian W Jul 10 '20 at 20:39
  • Do you know the specific list of files you would like to retrieve from your stash ? – LeGEC Jul 10 '20 at 21:00
  • @AdrianW Thanks for the tip. I will look into that. – Ryan Jul 10 '20 at 21:12
  • @LeGEC Yes, those files in my question. – Ryan Jul 10 '20 at 21:13
  • @AdrianW Thanks again for your comment. Would you mind seeing if you can clarify on my related question here? https://stackoverflow.com/q/62867041/470749 – Ryan Jul 12 '20 at 22:27

2 Answers2

2

If you are interested in the content of datastore/static/js/app/main.js, it looks like the stash has stored the action "delete this file". The content you wish to recover is maybe stored in the commit before the stash.

To make sense of the gibberish I wrote above :

From a terminal, run :

git log --oneline --graph stash@{0}

You will see that your stash is actually a commit (it just isn't stored in one of your branches), and that it has a parent commit (which was the state of your work at some point).

You can view the full content of the file in the stash :

git show stash@{0}:datastore/static/js/app/main.js

and the full content of the file in the "parent commit" :

git show <parentsha1>:datastore/static/js/app/main.js

If my guess is correct, the first "git show" will be empty.

Anyway, you can get the content you wish :

git checkout <the correct ref> -- datastore/static/js/app/main.js

and then use it however you want.

LeGEC
  • 46,477
  • 5
  • 57
  • 104
  • Thank you for this answer! You are correct that the first "git show" says "fatal: Path 'datastore/static/js/app/main.js' does not exist". How can I determine the `` for the second "git show" command? (I don't know how to interpret the output of `git log --oneline --graph stash@{0}`) https://imgur.com/a/FZPjJiX I think you will have saved me! – Ryan Jul 11 '20 at 18:06
  • I posted this as a full new question here: https://stackoverflow.com/q/62867041/470749 – Ryan Jul 12 '20 at 22:25
  • 1
    The "parent commit" would be `d4a48154` – LeGEC Jul 12 '20 at 23:08
  • Ahh, today `git checkout stash@{0} -- datastore/static/js/app/main.js` also worked instead of specifying the commit hash. That was convenient. – Ryan Aug 19 '20 at 20:21
1

You might come back to the version where you stashed from, stash pop, rename the file, then stash save again, then junp back to the revision you are currently working on and stash pop. Then rename should not be an issue

eftshift0
  • 26,375
  • 3
  • 36
  • 60