Home > OS >  Accidentally entered git stash pop instead of git stash--how to undo it?
Accidentally entered git stash pop instead of git stash--how to undo it?

Time:02-11

I know there are similar questions but I am very much new to the whole 'git' scene. So I wanted to ask this question with my own contextual situation.

So I use git for work, where I use SmartGit to stage and commit files, gitbash to push/pull, and polygerrit to submit those commits into my work's repo.

My usual steps are:

  1. open smartgit and select files in working tree to be staged
  2. select all staged files and commit
  3. enter description and click commit (I don't push from SmartGit because it's caused problems before)
  4. go to PolyGerrit, 1 the commit myself, then submit
  5. open gitbash and change directory
  6. do "git push"
  7. do "git stash"
  8. do "git pull" then "git push"
  9. do "git stash pop"
  10. then, I'm usually all good to go.

I'm sure there's something unnecessary I'm doing, or an easier way, but after many a merge conflict and other issues, these exact steps have worked fine for a while now. But today I accidentally typed "git stash pop" before using "git stash". PolyGerrit doesn't show any mergeconflicts, but gitbash does? Is there any way at all to undo the "git stash pop"?

The files are either .png, .md, .ai, or .svg (if it helps at all?)

Any help is much appreciated. Thanks!

CodePudding user response:

A general suggestion is to use git stash apply instead of git stash pop as the former is more forgiving as it keeps the stashed diff in the stash-list whereas the latter removes it after applying.

CodePudding user response:

First thing to do is to make a backup of the entire directory. Copy the whole lot to a thumb drive or at least another directory.

Step two is to find out what happened.

I would trust what git bash is telling you over over third party tools.

It could be that there is nothing to do. I created a scenario where popping the stash would create a merge conflict, as it sounds like you have.

  • One file checked in containing the String "aardvark".
  • A stash with the String "bat" in the file.
  • Working directory with the string "cat" in the file.

and this was the result of popping on the terminal:

$ git stash pop
error: Your local changes to the following files would be overwritten by merge:
    a.txt
Please commit your changes or stash them before you merge.
Aborting
The stash entry is kept in case you need it again.

I checked the log and it looks like the stash is still there:

$ git log --oneline --graph --all
*   b4ae4c0 (refs/stash) WIP on master: 9e38acc File contains aardvark.
|\  
| * c969465 index on master: 9e38acc File contains aardvark.
|/  
* 9e38acc (HEAD -> master) File contains aardvark.

I checked the diff between head and the stash and it looks as expected (hashes are copied from the above log output)

$ git diff 9e38acc b4ae4c0
diff --git a/a.txt b/a.txt
index c2625ca..1054901 100644
--- a/a.txt
    b/a.txt
@@ -1  1 @@
-aardvark
 bat

Finally, checked the diff on the working directory and it is still as expected.

$ git diff
diff --git a/a.txt b/a.txt
index c2625ca..ef07ddc 100644
--- a/a.txt
    b/a.txt
@@ -1  1 @@
-aardvark
 cat

You can use the above workflow to check what has gone on in your git repo. If it is like mine then there is nothing to do. Hopefully, that is the case.

Step three: If something has gone wrong, you will have more information to write a more specific question.

Step four: This is for the future. Practice good git hygiene. Commit often so that you never lose more than a couple of minutes work if something happens to your working directory. Push often so that you never lose more than half an hour of work if something happens to your computer.

  • Related