Skip to content

Commit 15fc1c0

Browse files
committed
Merge branch 'sg/stash-k-i'
* sg/stash-k-i: Documentation: tweak use case in "git stash save --keep-index" stash: introduce 'stash save --keep-index' option
2 parents e636799 + caf1899 commit 15fc1c0

File tree

3 files changed

+52
-6
lines changed

3 files changed

+52
-6
lines changed

Documentation/git-stash.txt

Lines changed: 22 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,24 @@ $ 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 # add just first part to the index
184+
$ git stash save --keep-index # save all other changes to the stash
185+
$ edit/build/test first part
186+
$ git commit foo -m 'First part' # commit fully tested change
187+
$ git stash pop # prepare to work on all other changes
188+
... repeat above five steps until one commit remains ...
189+
$ edit/build/test remaining parts
190+
$ git commit foo -m 'Remaining parts'
191+
----------------------------------------------------------------
192+
172193
SEE ALSO
173194
--------
174195
linkgit:git-checkout[1],

contrib/completion/git-completion.bash

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1163,8 +1163,19 @@ _git_show ()
11631163
_git_stash ()
11641164
{
11651165
local subcommands='save list show apply clear drop pop create'
1166-
if [ -z "$(__git_find_subcommand "$subcommands")" ]; then
1166+
local subcommand="$(__git_find_subcommand "$subcommands")"
1167+
if [ -z "$subcommand" ]; then
11671168
__gitcomp "$subcommands"
1169+
else
1170+
local cur="${COMP_WORDS[COMP_CWORD]}"
1171+
case "$subcommand,$cur" in
1172+
save,--*)
1173+
__gitcomp "--keep-index"
1174+
;;
1175+
*)
1176+
COMPREPLY=()
1177+
;;
1178+
esac
11681179
fi
11691180
}
11701181

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)