5

Need some help, I screwed up. Please reserve judgement.

Alright, this is what I did.

  1. git stash

  2. made some changes to the code.

  3. git stash (again)

  4. git stash apply

Is there any way I can get back to the code I had before the first git stash.

Any help would be appreciated.

Thanks, in advance.

SSK
  • 61
  • 5
  • 1
    "Is there any way I can get back to the code I had before the first git stash." - yes: `git reset $(commit-id-to-revert-to) --hard` – Dai Jun 25 '22 at 05:01
  • 2
    BTW, `stash apply` will keep the stash, [whereas](https://stackoverflow.com/questions/15286075/difference-between-git-stash-pop-and-git-stash-apply) `stash pop` discards it. – Dai Jun 25 '22 at 05:02
  • 1
    Use `git stash list` to see the entries you have in the stash, and inspect them with `git show stash@{}` – Romain Valeri Jun 25 '22 at 10:02

2 Answers2

1

Appreciate that applying or popping a Git stash just alters the working directory and/or stage. It does not make a new commit. Therefore, simply doing a hard reset should get you back to where you were before the first stash:

# from your branch
git reset --hard

That being said, if you wanted to retain some permutation of the changes since the first stash, that is another story and would require more work and thought to pull off.

Note: Generally speaking, popping a Git stash is risky, because if something goes wrong along the way, you can't reapply the same stash as it has already been popped from the stack. Use git stash apply for best results.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
1
git stash list
<shows all the saved stashes>

git stash show 'stash{0}'
<displays changes in this stash>
<change index according to the one you want, 0 is the last saved stash>

git stash apply 'stash{0}'
<applies the stash>
SSK
  • 61
  • 5