Is there a way to reorder Git stashes?

You can make several delays in Git:

git stash save "quick temp stash" git stash save "another quick temp stash" git stash save "This is important but I need to put it on the back burner" git stash save "This is almost certainly garbage, but just in case ..." 

Now I know that I can return them in any order:

 git stash pop stash@ {3} # recover quick temp stash git stash pop stash@ {2} # recover another quick temp stash 

But obviously, a more convenient syntax is preferable:

 git stash pop 

This is not just because there is less typing, but also because less thinking is required: if I have several delays, I should look at them, or maybe git stash show couple, until I find the one I want. But if I just keep the last “temp” burst at the top (followed by the next temp temp, etc.), I don’t have to think at all; I just pop .

So my question is, can I somehow change the order of my delays so that I can think when they need to “pop out” at the time when I save them, and not at the time when I “pop”, them. This would allow me to save “This is almost certainly rubbish, but just in case ...” and “This is important, but I need to put it in the background”, but at the end of the list, where they do not impede access to simpler quick records.

+5
source share
2 answers

As in Whymarrh’s Response and Donnie’s comment , I think you are probably best served by simply committing, I note, however, that:

If you really want to use prefixes and reorder them, you can configure refs/stash when you work ...

This can be done without using git stash pop . It is just hard. (Edit: I see, re-reading that this was an idea in Whymarrh's answer.)

The reflog file, .git/logs/refs/stash , contains reflog entries 1 through N (as many of them exist). The stash link itself contains null input.

Operation

A drop consists of deleting a specific reflog entry ( git reflog delete knows how to handle a special null register):

 drop_stash () { assert_stash_ref " $@ " git reflog delete --updateref --rewrite "${REV}" && say "$(eval_gettext "Dropped \${REV} (\$s)")" || die "$(eval_gettext "\${REV}: Could not drop stash entry")" # clear_stash if we just dropped the last stash entry git rev-parse --verify --quiet " $ref_stash@ {0}" >/dev/null || clear_stash } 

(where clear_stash removes the refs/stash ). The $REV argument is either refs/ stash@ {N} or refs/stash , unless you specify one.

The store operation inserts a record to zero using git update-ref :

 [snip] w_commit="$1" if test -z "$stash_msg" then stash_msg="Created via \"git stash store\"." fi git update-ref --create-reflog -m "$stash_msg" $ref_stash $w_commit ret=$? test $ret != 0 && test -z "$quiet" && die "$(eval_gettext "Cannot update \$ref_stash with \$w_commit")" return $ret 

Thus, it is possible, albeit a little more difficult, to perform the “roll” operation (if you are familiar with Forth or Postscript). For example, to roll the bottom three inputs one step by changing:

 E stash@ {4} D stash@ {3} C stash@ {2} B stash@ {1} A stash@ {0} 

in

 E stash@ {4} D stash@ {3} B stash@ {2} A stash@ {1} C stash@ {0} 

you just copied C to the bottom as if through a new store , and then release stash@ {3} (which has moved due to pasting to zero).

In fact, you can do this by running git stash -q store and git stash -q drop with the corresponding arguments.

+2
source

Out of the box - I don't think stash was really meant for this workflow.

However, if you switch to temporary commits, you can reorder these commits as you like, and cherry-pick commits (e.g. git cherry-pick $tempbranch as a replacement for git stash pop ).

If you really want to use stats and reorder them, you can configure refs/stash as you work (through some scripts). From git-stash documentation :

The last file you created is stored in refs/stash ; older stashes are found in the reflog of this link and can be named using the usual reflog syntax (for example, stash@ {0} is the last created stamp, stash@ {1} is the one before it, stash@ {2.hours.ago} also possible). Stamps can also be referenced by specifying only the stash index (for example, the integer n equivalent to stash@ {n} ).

If you decide to write a script for your workflow, you also have git stash create and git stash store at your disposal:

create

Create a stash (which is a regular commit object) and return its object name without storing it anywhere in the ref namespace. It is intended for use in scripts. This is probably not the command you want to use; see "save" above.

score

Save the given wallet created with git stash create (which is a cheating merge) in stash ref, updating stash reflog. It is intended for use in scripts. This is probably not the command you want to use; see "save" above.

+3
source

Source: https://habr.com/ru/post/1268140/


All Articles