Search code examples
gitgit-stash

I misplaced some work, but I know its somewhere in my git-stash stack


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.


Solution

  • 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>