Search code examples
bashgitvimversion-controlgit-config

Always prompt for a stash message in git


I tend to stash changes without remembering why I stash them.

I do make it a point to git stash push -m most of the time, but if there's a fire drill or something else that knocks me out of flow, I may forget and lose time trying to recover.

Is there a way to imitate the behavior of git commit (minus the -m) for git stash where vim pops up and abandons the operation if the message is empty?


Solution

  • AFAIK there's no config option for this. You'll have to write an alias in your .gitconfig and train yourself to use it.

    For example, I have two stash aliases git pop and git save. (You can see I didn't get the memo about git stash save being deprecated). These are both for convenience, and to change the default behavior to something I find more useful.

    save = stash save -k -u
    pop = stash pop
    

    Unfortunately git stash push -m doesn't bring up an editor, if you need to write more than a few words to describe what you were doing consider a branch instead. We can fix this by writing a little shell function and passing the argument to -m using "$@" to ensure messages with spaces are a single argument.

    savem = "!f() { git save -m \"$@\"; }; f"
    

    Now you can write git savem 'remember to remember what this was'.

    $ git savem 'remember to remember what this was'
    Saved working directory and index state On issue/45: remember to remember what this was
    

    And if you forget, you'll get the normal git-stash usage message. You can snazz up the alias to provide a custom usage message if you like.

    $ git savem
    usage: git stash list [<options>]
       or: git stash show [<stash>]
    ...