diff --git a/Documentation/git-stash.txt b/Documentation/git-stash.txt index 1c4ed412a1..30a249cbc5 100644 --- a/Documentation/git-stash.txt +++ b/Documentation/git-stash.txt @@ -14,7 +14,6 @@ SYNOPSIS 'git stash' ( pop | apply ) [--index] [-q|--quiet] [] 'git stash' branch [] 'git stash' [save [--patch] [-k|--[no-]keep-index] [-q|--quiet] []] -'git stash' [-p|--patch|-k|--keep-index] 'git stash' clear 'git stash' create @@ -46,9 +45,11 @@ OPTIONS save [--patch] [--[no-]keep-index] [-q|--quiet] []:: Save your local modifications to a new 'stash', and run `git reset - --hard` to revert them. This is the default action when no - subcommand is given. The part is optional and gives - the description along with the stashed state. + --hard` to revert them. The part is optional and gives + the description along with the stashed state. For quickly making + a snapshot, you can omit _both_ "save" and , but giving + only does not trigger this action to prevent a misspelled + subcommand from making an unwanted stash. + If the `--keep-index` option is used, all changes already added to the index are left intact. diff --git a/git-stash.sh b/git-stash.sh index 9fd72894c4..f24337613b 100755 --- a/git-stash.sh +++ b/git-stash.sh @@ -8,7 +8,6 @@ USAGE="list [] or: $dashless ( pop | apply ) [--index] [-q|--quiet] [] or: $dashless branch [] or: $dashless [save [-k|--keep-index] [-q|--quiet] []] - or: $dashless [-k|--keep-index] or: $dashless clear" SUBDIRECTORY_OK=Yes @@ -146,6 +145,14 @@ save_stash () { -q|--quiet) GIT_QUIET=t ;; + --) + shift + break + ;; + -*) + echo "error: unknown option for 'stash save': $1" + usage + ;; *) break ;; @@ -355,6 +362,18 @@ apply_to_branch () { drop_stash $stash } +# The default command is "save" if nothing but options are given +seen_non_option= +for opt +do + case "$opt" in + -*) ;; + *) seen_non_option=t; break ;; + esac +done + +test -n "$seen_non_option" || set "save" "$@" + # Main command set case "$1" in list) @@ -406,9 +425,9 @@ branch) apply_to_branch "$@" ;; *) - case $#,"$1","$2" in - 0,,|1,-k,|1,--keep-index,|1,-p,|1,--patch,|2,-p,--no-keep-index|2,--patch,--no-keep-index) - save_stash "$@" && + case $# in + 0) + save_stash && say '(To restore them type "git stash apply")' ;; *) diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh index e16ad93d2c..5514f74b30 100755 --- a/t/t3903-stash.sh +++ b/t/t3903-stash.sh @@ -208,4 +208,15 @@ test_expect_success 'stash -k' ' test bar,bar4 = $(cat file),$(cat file2) ' +test_expect_success 'stash --invalid-option' ' + echo bar5 > file && + echo bar6 > file2 && + git add file2 && + test_must_fail git stash --invalid-option && + test_must_fail git stash save --invalid-option && + test bar5,bar6 = $(cat file),$(cat file2) && + git stash -- -message-starting-with-dash && + test bar,bar2 = $(cat file),$(cat file2) +' + test_done