2

I was in the middle of fixing a feature when another issue of greater priority arose. Instead of creating a new branch, committing, and moving back to the master branch like I usually do, I used git stash. I forgot that I never finished the fix, and just left it sitting in my stash. I often use git stash to move changes around between branches set up with different project configurations to test against multiple build environments, so I'm always stashing things. Some time later I realized that the fix wasn't anywhere to be found in the master branch, and I remembered that I stashed it a while ago. I know for a fact that I still must have it, as I always use git stash apply instead of git stash pop, but I don't have any idea how far back in the stack these changes are. I do, however, know that all of the changes for this fix were made in a single source file, if that helps.

Is there a "best" way to try to find this lost fix? I could just run git stash pop, see if it restores the fix, git reset --hard if it doesn't, git stash drop if there was a merge conflict, and repeat until I find it, but I was hoping for something a little less destructive. It always seems like with Git, there's at least 10 different ways to accomplish anything you might want to do in terms of managing your code, so I'd like to get a second opinion before I start demolishing my stash stack.

n00neimp0rtant
  • 905
  • 1
  • 8
  • 18
  • 1
    `git stash` accepts an optional argument to give your stash a commit message: `git stash save "message"`. If you give every stash a decent message, it'll be easier to find them later. See also [this answer](http://stackoverflow.com/a/11688523/4896941) for more fun with named stashes. – Lithis May 20 '15 at 19:05

1 Answers1

6

First use

git stash list

to get an overview of all the items you have stashed (and obtain their hashes).

To see the changes they introduce use

git stash show <stash_hash>

To see a full diff of those changes use

git stash show -p <stash_hash>

Once you known which item you wish to apply use:

git stash apply <stash_hash>
n00neimp0rtant
  • 905
  • 1
  • 8
  • 18
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • This definitely seems better than the idea I had, but is there a way to look at a stash's diffs without applying it first? – n00neimp0rtant May 20 '15 at 18:29
  • As I've told: `git stash show `. First obtain the hash trough `git stash list`. Then use it in `git stash show `. – hek2mgl May 20 '15 at 18:29
  • This answer is what you want to do in this case, but there is another way to find "lost" things in Git as well—`git reflog`. I won't go into all the details here—there are plenty of tutorials online—but you could use `git reflog` to recover a stash even after deleting it (or to recover other lost commits, for example after running `git reset` and moving your branch to an earlier commit). – Lithis May 20 '15 at 18:30
  • Use `git stash show -p ` to see a diff. – Lithis May 20 '15 at 18:32
  • Also, you don't need to use the commit hash as an argument to `git stash show`. You can also use `stash@{n}` where `n` is an integer, just as it appears in the output of `git stash list`. – Lithis May 20 '15 at 18:34
  • You are welcome! :) @Lithis I appreciated your comments. Thanks for that! – hek2mgl May 20 '15 at 18:40