Skip to content

Commit 7bedebc

Browse files
szedergitster
authored andcommitted
stash: introduce 'stash save --keep-index' option
'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>
1 parent 6991357 commit 7bedebc

File tree

3 files changed

+51
-6
lines changed

3 files changed

+51
-6
lines changed

Documentation/git-stash.txt

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,15 @@ is also possible).
3636
OPTIONS
3737
-------
3838

39-
save [<message>]::
39+
save [--keep-index] [<message>]::
4040

4141
Save your local modifications to a new 'stash', and run `git reset
4242
--hard` to revert them. This is the default action when no
4343
subcommand is given. The <message> part is optional and gives
4444
the description along with the stashed state.
45+
+
46+
If the `--keep-index` option is used, all changes already added to the
47+
index are left intact.
4548

4649
list [<options>]::
4750

@@ -169,6 +172,23 @@ $ git stash apply
169172
... continue hacking ...
170173
----------------------------------------------------------------
171174

175+
Testing partial commits::
176+
177+
You can use `git stash save --keep-index` when you want to make two or
178+
more commits out of the changes in the work tree, and you want to test
179+
each change before committing:
180+
+
181+
----------------------------------------------------------------
182+
... hack hack hack ...
183+
$ git add --patch foo
184+
$ git stash save --keep-index
185+
$ build && run tests
186+
$ git commit -m 'First part'
187+
$ git stash apply
188+
$ build && run tests
189+
$ git commit -a -m 'Second part'
190+
----------------------------------------------------------------
191+
172192
SEE ALSO
173193
--------
174194
linkgit:git-checkout[1],

contrib/completion/git-completion.bash

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1137,8 +1137,19 @@ _git_show ()
11371137
_git_stash ()
11381138
{
11391139
local subcommands='save list show apply clear drop pop create'
1140-
if [ -z "$(__git_find_subcommand "$subcommands")" ]; then
1140+
local subcommand="$(__git_find_subcommand "$subcommands")"
1141+
if [ -z "$subcommand" ]; then
11411142
__gitcomp "$subcommands"
1143+
else
1144+
local cur="${COMP_WORDS[COMP_CWORD]}"
1145+
case "$subcommand,$cur" in
1146+
save,--*)
1147+
__gitcomp "--keep-index"
1148+
;;
1149+
*)
1150+
COMPREPLY=()
1151+
;;
1152+
esac
11421153
fi
11431154
}
11441155

git-stash.sh

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ create_stash () {
8686
}
8787

8888
save_stash () {
89+
keep_index=
90+
case "$1" in
91+
--keep-index)
92+
keep_index=t
93+
shift
94+
esac
95+
8996
stash_msg="$1"
9097

9198
if no_changes
@@ -104,6 +111,13 @@ save_stash () {
104111
git update-ref -m "$stash_msg" $ref_stash $w_commit ||
105112
die "Cannot save the current status"
106113
printf 'Saved working directory and index state "%s"\n' "$stash_msg"
114+
115+
git reset --hard
116+
117+
if test -n "$keep_index" && test -n $i_tree
118+
then
119+
git read-tree --reset -u $i_tree
120+
fi
107121
}
108122

109123
have_stash () {
@@ -153,7 +167,8 @@ apply_stash () {
153167
die "$*: no valid stashed state found"
154168

155169
unstashed_index_tree=
156-
if test -n "$unstash_index" && test "$b_tree" != "$i_tree"
170+
if test -n "$unstash_index" && test "$b_tree" != "$i_tree" &&
171+
test "$c_tree" != "$i_tree"
157172
then
158173
git diff-tree --binary $s^2^..$s^2 | git apply --cached
159174
test $? -ne 0 &&
@@ -235,7 +250,7 @@ show)
235250
;;
236251
save)
237252
shift
238-
save_stash "$*" && git-reset --hard
253+
save_stash "$*"
239254
;;
240255
apply)
241256
shift
@@ -268,8 +283,7 @@ pop)
268283
if test $# -eq 0
269284
then
270285
save_stash &&
271-
echo '(To restore them type "git stash apply")' &&
272-
git-reset --hard
286+
echo '(To restore them type "git stash apply")'
273287
else
274288
usage
275289
fi

0 commit comments

Comments
 (0)