fredefox
fredefox

Reputation: 711

Prevent `git stash` with no additional arguments from running

Anyone know if it's possible to modify git so it will simply reject git stash and in stead require me to explicitly specify git stash push. I often find myself accidently stashing stuff when I wanna inspect the stash (git stash list). Similarly it's rather annoying if I have stuff in the index and then do git stash (and forget to add --keep-index). Which in turn can have disastrous consequences (How to recover the index after a git stash / git stash pop?)

Upvotes: 3

Views: 259

Answers (2)

tomi
tomi

Reputation: 173

I'd wish git stash worked as git stash list by default (I often typo tig stash as git stash and then get mildy surprised/annoyed about it)

I've been thinking this wrapper thing, but decided against - maybe it makes it less likely to get the unwanted git stash push to happen -- so my take on this (tested in bash, dash and zsh):

git () ( #set -x
   git=`command -vp git` || { echo "'git': command not found" >&2; exit 1; }
   test $#"${1-}" = 1stash && set -- stash list
   exec "$git" "$@"
)
  • executes in subshell (as git () ( ... ))
  • command -vp needed for zsh, otherwise resolves $git as 'git'
    • bash and dash can do without -p (searching given, not fixed $PATH)
  • $#"${1-}" instead of "$#${1-}" as zsh resolved latter as 5'${1-}' :O

since needing -p for zsh, one with alternative PATH search:

git () ( #set -x
   IFS=:; git=
   for d in ${=PATH}
   do test -x $d/git || continue
      git=$d/git; break
   done
   test "$git" || { echo "'git': command not found" >&2; exit 1; }
   test $#"${1-}" = 1stash && set -- stash list
   exec "$git" "$@"
)

same works in bash and dash when ${=PATH} is changed to $PATH

Upvotes: 0

ElpieKay
ElpieKay

Reputation: 30858

Add the function to ~/.bashrc.

function git() {
  GIT=`which git`
  if [[ "$#" -eq 1 ]] && [[ "$1" = "stash" ]];then
    echo 'WARNING: run "git stash push" instead.'
  else
    $GIT $@
  fi
}

Though I tried on Ubuntu and Windows and both succeeded, I'm not sure if there's any side effect that may cause bugs.

Upvotes: 7

Related Questions