Skip to content

Commit

Permalink
stash: introduce 'stash save --keep-index' option
Browse files Browse the repository at this point in the history
'git stash save' saves local modifications to a new stash, and runs 'git
reset --hard' to revert them to a clean index and work tree.  When the
'--keep-index' option is specified, after that 'git reset --hard' the
previous contents of the index is restored and the work tree is updated
to match the index.  This option is useful if the user wants to commit
only parts of his local modifications, but wants to test those parts
before committing.

Also add support for the completion of the new option, and add an
example use case to the documentation.

Signed-off-by: SZEDER Gábor <szeder@ira.uka.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
szeder authored and gitster committed Jul 5, 2008
1 parent 6991357 commit 7bedebc
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
22 changes: 21 additions & 1 deletion Documentation/git-stash.txt
Expand Up @@ -36,12 +36,15 @@ is also possible).
OPTIONS
-------

save [<message>]::
save [--keep-index] [<message>]::

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 <message> part is optional and gives
the description along with the stashed state.
+
If the `--keep-index` option is used, all changes already added to the
index are left intact.

list [<options>]::

Expand Down Expand Up @@ -169,6 +172,23 @@ $ git stash apply
... continue hacking ...
----------------------------------------------------------------

Testing partial commits::

You can use `git stash save --keep-index` when you want to make two or
more commits out of the changes in the work tree, and you want to test
each change before committing:
+
----------------------------------------------------------------
... hack hack hack ...
$ git add --patch foo
$ git stash save --keep-index
$ build && run tests
$ git commit -m 'First part'
$ git stash apply
$ build && run tests
$ git commit -a -m 'Second part'
----------------------------------------------------------------

SEE ALSO
--------
linkgit:git-checkout[1],
Expand Down
13 changes: 12 additions & 1 deletion contrib/completion/git-completion.bash
Expand Up @@ -1137,8 +1137,19 @@ _git_show ()
_git_stash ()
{
local subcommands='save list show apply clear drop pop create'
if [ -z "$(__git_find_subcommand "$subcommands")" ]; then
local subcommand="$(__git_find_subcommand "$subcommands")"
if [ -z "$subcommand" ]; then
__gitcomp "$subcommands"
else
local cur="${COMP_WORDS[COMP_CWORD]}"
case "$subcommand,$cur" in
save,--*)
__gitcomp "--keep-index"
;;
*)
COMPREPLY=()
;;
esac
fi
}

Expand Down
22 changes: 18 additions & 4 deletions git-stash.sh
Expand Up @@ -86,6 +86,13 @@ create_stash () {
}

save_stash () {
keep_index=
case "$1" in
--keep-index)
keep_index=t
shift
esac

stash_msg="$1"

if no_changes
Expand All @@ -104,6 +111,13 @@ save_stash () {
git update-ref -m "$stash_msg" $ref_stash $w_commit ||
die "Cannot save the current status"
printf 'Saved working directory and index state "%s"\n' "$stash_msg"

git reset --hard

if test -n "$keep_index" && test -n $i_tree
then
git read-tree --reset -u $i_tree
fi
}

have_stash () {
Expand Down Expand Up @@ -153,7 +167,8 @@ apply_stash () {
die "$*: no valid stashed state found"

unstashed_index_tree=
if test -n "$unstash_index" && test "$b_tree" != "$i_tree"
if test -n "$unstash_index" && test "$b_tree" != "$i_tree" &&
test "$c_tree" != "$i_tree"
then
git diff-tree --binary $s^2^..$s^2 | git apply --cached
test $? -ne 0 &&
Expand Down Expand Up @@ -235,7 +250,7 @@ show)
;;
save)
shift
save_stash "$*" && git-reset --hard
save_stash "$*"
;;
apply)
shift
Expand Down Expand Up @@ -268,8 +283,7 @@ pop)
if test $# -eq 0
then
save_stash &&
echo '(To restore them type "git stash apply")' &&
git-reset --hard
echo '(To restore them type "git stash apply")'
else
usage
fi
Expand Down

0 comments on commit 7bedebc

Please sign in to comment.