A better git stash

Jon of saintsjd recently wrote a piece on improving the git user interface. This got me thinking about one of my git peeves.

git stash needs another sub-command. Often I want to stash ALL unstaged changes, including new (not yet added) files. This is almost possible with git stash -k, except it does not stash new files, which is very important.

Enter git stash unstaged. So now I’ve stashed the unstaged changes. That is, I’ve stashed all the changes I do not want included in the next commit. I can now run my tests as I always do before I commit… That’s what you do too, right? Oops, the tests fail because I forgot to add a file, which now happens to be in the stash. At this point I do not know of a way to get my stashed changes back exactly as they were before I stashed them, that is, unstaged. It’s really bad if there are some files that are partially staged (i.e., only certain hunks were staged), because they will create conflicts if I attempt to apply the stash right now. This should be possible. There should be a way to get things back exactly as they were before I stashed them. The missing command is git stash pop unstaged, which applies all of the changes from the stash and puts them in the unstaged (off-stage? pre-stage?) area, without conflicts.

In summary:

  • git stash unstaged – stash all changes that are not currently staged, including new and removed files
  • git stash pop unstaged – pop the top stash as unstaged changes

This would greatly improve my workflow when I get over-zealous and start changing more than I want to commit at one time (happens all the time). It would make it much easier to isolate those changes into individual, logical commits.

3 Responses to “A better git stash”

- scott sadler

Try this for git stash pop unstaged:

git diff -R stash@{0} | patch -s -p1 && git stash drop -q

- daniel

Thanks, Scott. I’ll have to give that a try.

- moudy

‘git stash -u’ will stash untracked files, so I think ‘git stash -k -u’ is what you’re looking for.

Leave a Reply