Implement "git stash branch <newbranch> <stash>"
Restores the stashed state on a new branch rooted at the commit on which the stash was originally created, so that conflicts caused by subsequent changes on the original branch can be dealt with. (Thanks to Junio for this nice idea.) Signed-off-by: Abhijit Menon-Sen <ams@toroid.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Abhijit Menon-Sen committed
Jul 3, 2008 at 11:46 UTC
656b50345239293929ad8c639c5f1941c6b867ad
2 files changed
+39
-1
Documentation/git-stash.txt
+18
-1
@@ -8,8 +8,11 @@ git-stash - Stash the changes in a dirty working directory away
8
SYNOPSIS
9
--------
10
[verse]
11
-'git stash' (list | show [<stash>] | apply [<stash>] | clear | drop [<stash>] | pop [<stash>])
11
+'git stash' list
12
+'git stash' (show | apply | drop | pop ) [<stash>]
13
+'git stash' branch <branchname> [<stash>]
14
'git stash' [save [<message>]]
15
+'git stash' clear
16
17
DESCRIPTION
18
-----------
@@ -81,6 +84,20 @@ tree's changes, but also the index's ones. However, this can fail, when you
84
have conflicts (which are stored in the index, where you therefore can no
85
longer apply the changes as they were originally).
86
87
+branch <branchname> [<stash>]::
88
+
89
+ Creates and checks out a new branch named `<branchname>` starting from
90
+ the commit at which the `<stash>` was originally created, applies the
91
+ changes recorded in `<stash>` to the new working tree and index, then
92
+ drops the `<stash>` if that completes successfully. When no `<stash>`
93
+ is given, applies the latest one.
94
++
95
+This is useful if the branch on which you ran `git stash save` has
96
+changed enough that `git stash apply` fails due to conflicts. Since
97
+the stash is applied on top of the commit that was HEAD at the time
98
+`git stash` was run, it restores the originally stashed state with
99
+no conflicts.
100
+
101
clear::
102
Remove all the stashed states. Note that those states will then
103
be subject to pruning, and may be difficult or impossible to recover.
git-stash.sh
+21
@@ -218,6 +218,23 @@ drop_stash () {
218
git rev-parse --verify "$ref_stash@{0}" > /dev/null 2>&1 || clear_stash
219
}
220
221
+apply_to_branch () {
222
+ have_stash || die 'Nothing to apply'
223
+
224
+ test -n "$1" || die 'No branch name specified'
225
+ branch=$1
226
+
227
+ if test -z "$2"
228
+ then
229
+ set x "$ref_stash@{0}"
230
+ fi
231
+ stash=$2
232
+
233
+ git-checkout -b $branch $stash^ &&
234
+ apply_stash --index $stash &&
235
+ drop_stash $stash
236
+}
237
+
238
# Main command set
239
case "$1" in
240
list)
@@ -264,6 +281,10 @@ pop)
281
drop_stash "$@"
282
fi
283
;;
284
+branch)
285
+ shift
286
+ apply_to_branch "$@"
287
+ ;;
288
*)
289
if test $# -eq 0
290
then