Is there a way to re-order Git stashes?

Viewed 2263

In Git one can make multiple stashes:

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 get those stashes back in any order:

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

But obviously the more convenient syntax is preferable:

git stash pop

That's not just because there's less to type, but also because there's less thinking required: if I have several stashes I have to look through them, and maybe git stash show a couple, until I find the one I want. But if I just keep the most recent "temp" stash at the top (followed by the next temp stash, etc.) I don't have to think at all; I just pop.

So, my question is, is there any way I can re-order my stashes, so that I can think about when they should be "popped" at the time I save them, rather than at the time I "pop" them. This would allow me to still save "This is almost certainly garbage, but just in case ..." and "This is important but I need to put it on the back burner" stashes, but in the back of the stash list where they don't complicate accessing simpler quick stashes.

3 Answers

It's therefore possible, albeit a bit tricky, to achieve a "roll" operation (if you're familiar with Forth or Postscript).
To roll the bottom three entries up one step, you would just copy C to the bottom, as if via a new store, then drop stash@{3} (which moved up because of the insertion at zero).

If you do this, make sure to use Git 2.36 (Q2 2022), as it would be too slow if you were to roll out a large number of commits.

"git stash drop"(man) is reimplemented as an internal call to reflog_delete() function, instead of invoking "git reflog delete"(man)via run_command() API.

So instead of a subshell for each stash drop => reflog delete, all those stash drop calls remain in the same process, using internal functions.

See commit 758b4d2, commit 7d3d226, commit 76bccbc (02 Mar 2022) by John Cai (john-cai).
(Merged by Junio C Hamano -- gitster -- in commit a2fc9c3, 16 Mar 2022)

reflog: libify delete reflog function and helpers

Helped-by: Ævar Arnfjörð Bjarmason
Signed-off-by: John Cai

Currently stash shells out to reflog in order to delete refs.
In an effort to reduce how much we shell out to a subprocess, libify the functionality that stash needs into reflog.c.

Add a reflog_delete function that is pretty much the logic in the while loop in builtin/reflog.c cmd_reflog_delete().
This is a function that builtin/reflog.c and builtin/stash.c can both call.

Related