From: "SZEDER Gábor" <szeder@ira.uka.de>
To: "Björn Steinbrink" <B.Steinbrink@gmx.de>
Cc: Robert Anderson <rwa000@gmail.com>,
Git Mailing List <git@vger.kernel.org>
Subject: [PATCH/RFC] stash: introduce 'stash save --keep-index' option
Date: Fri, 27 Jun 2008 16:37:15 +0200 [thread overview]
Message-ID: <20080627143715.GD6747@neumann> (raw)
In-Reply-To: <20080627071014.GA12344@atjola.homenet>
'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>
---
On Fri, Jun 27, 2008 at 09:10:14AM +0200, Björn Steinbrink wrote:
> Hm, I use "stash" for that purpose, which leads to kind of the reverse
> of your approach. So I do sth. like this:
>
> - hack hack hack
> - Notice that I want to make two commits out of what I have in my
> working tree
> - git add -p -- stage what I want in the first commit
> - git commit -m tmp -- temporary commit
> - git stash -- stash away what doesn't belong in the first commit
> - git reset HEAD^ -- drop the temporary commit, with the changes kept
> in the working tree
> - test, fix bugs, read the diff, whatever
> - git commit -- this time for good
> - git stash apply -- get back the changes for the second commit
I used to do the same, so I have added a '--keep-index' option to 'git
stash save' to simplify this workflow. Have a look at the use case
added to the documentation to see, how you could spare the temporary
commit and the 'reset HEAD^'.
RFC, because I'm not quite confident with using plumbing like 'git
read-tree'... and there are no tests.
Documentation/git-stash.txt | 22 +++++++++++++++++++++-
contrib/completion/git-completion.bash | 13 ++++++++++++-
git-stash.sh | 22 ++++++++++++++++++----
3 files changed, 51 insertions(+), 6 deletions(-)
diff --git a/Documentation/git-stash.txt b/Documentation/git-stash.txt
index baa4f55..936864f 100644
--- a/Documentation/git-stash.txt
+++ b/Documentation/git-stash.txt
@@ -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>]::
@@ -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],
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index ebf7cde..9a15500 100755
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -1139,8 +1139,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
}
diff --git a/git-stash.sh b/git-stash.sh
index 4938ade..92531a2 100755
--- a/git-stash.sh
+++ b/git-stash.sh
@@ -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
@@ -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 () {
@@ -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 &&
@@ -235,7 +250,7 @@ show)
;;
save)
shift
- save_stash "$*" && git-reset --hard
+ save_stash "$*"
;;
apply)
shift
@@ -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
--
1.5.6.1.95.ge713
next prev parent reply other threads:[~2008-06-27 14:38 UTC|newest]
Thread overview: 57+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-06-27 6:50 An alternate model for preparing partial commits Robert Anderson
2008-06-27 7:10 ` Björn Steinbrink
2008-06-27 14:37 ` SZEDER Gábor [this message]
2008-06-27 18:26 ` [PATCH/RFC] stash: introduce 'stash save --keep-index' option Junio C Hamano
2008-06-27 16:54 ` An alternate model for preparing partial commits Robert Anderson
2008-06-27 17:27 ` Björn Steinbrink
2008-06-27 17:34 ` Robert Anderson
2008-06-27 8:35 ` Johannes Sixt
2008-06-27 17:01 ` Robert Anderson
2008-06-27 8:50 ` Petr Baudis
2008-06-27 17:02 ` Robert Anderson
2008-06-27 13:33 ` Johannes Schindelin
2008-06-27 13:49 ` Miklos Vajna
2008-06-27 17:14 ` Robert Anderson
2008-06-27 17:45 ` Johannes Schindelin
2008-06-27 17:49 ` Robert Anderson
[not found] ` <alpine.DEB.1.00.0806271854120.9925@racer>
2008-06-27 18:07 ` Robert Anderson
2008-06-27 18:20 ` Dana How
2008-06-27 20:31 ` Stephen Sinclair
[not found] ` <willow-jeske-01l7H4tHFEDjCgPV-01l7ZhB8FEDjCdJs>
2008-06-27 20:45 ` David Jeske
2008-06-27 20:45 ` David Jeske
2008-06-28 17:23 ` Wincent Colaiuta
2008-06-28 2:14 ` Dmitry Potapov
2008-06-28 2:57 ` Robert Anderson
2008-06-28 4:03 ` Dmitry Potapov
[not found] ` <9af502e50806272320p23f01e8eo4a67c5f6f4476098@mail.gmail.com>
2008-06-28 6:31 ` Fwd: " Robert Anderson
2008-06-28 12:38 ` Dmitry Potapov
[not found] ` <20080628123522.GL5737@dpotapov.dyndns.org>
2008-06-28 15:53 ` Robert Anderson
2008-06-28 16:52 ` Dmitry Potapov
2008-06-27 18:15 ` Junio C Hamano
2008-06-27 18:43 ` Robert Anderson
2008-06-28 5:03 ` Jeff King
2008-06-28 7:03 ` Robert Anderson
2008-06-28 8:53 ` Jeff King
2008-06-28 21:53 ` Junio C Hamano
2008-06-28 14:51 ` Johannes Schindelin
2008-07-08 4:58 ` Jeff King
[not found] ` <willow-jeske-01l7H4tHFEDjCgPV-01l7H4sOFEDjCbyi>
2008-06-27 20:29 ` David Jeske
2008-06-27 20:47 ` Jakub Narebski
[not found] ` <willow-jeske-01l7H4tHFEDjCgPV-01l7[1OFFEDjCYJV>
2008-06-27 20:51 ` David Jeske
2008-06-27 20:51 ` David Jeske
[not found] ` <-8386235276716376372@unknownmsgid>
2008-06-27 22:55 ` Robert Anderson
2008-06-27 23:14 ` Junio C Hamano
2008-06-28 0:08 ` Robert Anderson
2008-06-28 2:57 ` Dmitry Potapov
2008-06-28 3:31 ` Robert Anderson
2008-06-28 14:34 ` Stephen Sinclair
2008-06-28 16:00 ` Robert Anderson
2008-06-28 16:30 ` Robert Anderson
2008-06-28 17:12 ` Jakub Narebski
2008-06-28 18:25 ` Robert Anderson
[not found] ` <willow-jeske-01l7H4tHFEDjCgPV-01l82hwbFEDjCX70>
2008-06-28 19:12 ` David Jeske
2008-06-28 19:12 ` David Jeske
2008-06-28 19:13 ` Stephen Sinclair
[not found] ` <willow-jeske-01l7H4tHFEDjCgPV-01l7buicFEDjCagd>
2008-06-28 0:22 ` David Jeske
2008-06-28 0:22 ` David Jeske
2008-06-27 20:29 ` David Jeske
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20080627143715.GD6747@neumann \
--to=szeder@ira.uka.de \
--cc=B.Steinbrink@gmx.de \
--cc=git@vger.kernel.org \
--cc=rwa000@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).