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>
SZEDER Gábor committed
Jun 27, 2008 at 16:37 UTC
7bedebcaad351108a8f6eab6a031f2be2c06b613
3 files changed
+51
-6
Documentation/git-stash.txt
+21
-1
@@ -36,12 +36,15 @@ is also possible).
36
OPTIONS
37
-------
38
39
-save [<message>]::
39
+save [--keep-index] [<message>]::
40
41
Save your local modifications to a new 'stash', and run `git reset
42
--hard` to revert them. This is the default action when no
43
subcommand is given. The <message> part is optional and gives
44
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.
48
49
list [<options>]::
50
@@ -169,6 +172,23 @@ $ git stash apply
172
... continue hacking ...
173
----------------------------------------------------------------
174
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
+
192
SEE ALSO
193
--------
194
linkgit:git-checkout[1],
contrib/completion/git-completion.bash
+12
-1
@@ -1137,8 +1137,19 @@ _git_show ()
1137
_git_stash ()
1138
{
1139
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
1142
__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
1153
fi
1154
}
1155
git-stash.sh
+18
-4
@@ -86,6 +86,13 @@ create_stash () {
86
}
87
88
save_stash () {
89
+ keep_index=
90
+ case "$1" in
91
+ --keep-index)
92
+ keep_index=t
93
+ shift
94
+ esac
95
+
96
stash_msg="$1"
97
98
if no_changes
@@ -104,6 +111,13 @@ save_stash () {
111
git update-ref -m "$stash_msg" $ref_stash $w_commit ||
112
die "Cannot save the current status"
113
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
121
}
122
123
have_stash () {
@@ -153,7 +167,8 @@ apply_stash () {
167
die "$*: no valid stashed state found"
168
169
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"
172
then
173
git diff-tree --binary $s^2^..$s^2 | git apply --cached
174
test $? -ne 0 &&
@@ -235,7 +250,7 @@ show)
250
;;
251
save)
252
shift
238
- save_stash "$*" && git-reset --hard
253
+ save_stash "$*"
254
;;
255
apply)
256
shift
@@ -268,8 +283,7 @@ pop)
283
if test $# -eq 0
284
then
285
save_stash &&
271
- echo '(To restore them type "git stash apply")' &&
272
- git-reset --hard
286
+ echo '(To restore them type "git stash apply")'
287
else
288
usage
289
fi