Reference git stash by number without stash@{i}
Asked Answered
C

2

1

I'm using git stashes extensively. Sometimes it becomes annoying to type out stash@{3} when, at first glance, a simple 3 should suffice. Is it possible to use the shorter reference somehow?

I know a solution with shell aliases. Making an alias like

sshow = "!f { git stash show @{$@}; }; f"

allows using git sshow 1. It even allows passing additional arguments to git stash show if $@ is expanded properly.

However, in this case git autocomplete does not work: when git stash show stash@{0} --<Tab><Tab> is typed it should show all git diff options, but here it knows nothing about the underlying command.

Are there other ways of making such alias which preserve original git diff completion context?

Corri answered 30/5, 2018 at 18:54 Comment(1)
Try to do git stash --<Tab><Tab> and make a right conclusion after.Heliotherapy
S
4

Yes, this is built-in to Git as of v2.11. You can refer to any stash by index only. For example, to apply the stash at index 2 you can type

git stash apply 2
Superheat answered 30/5, 2018 at 19:4 Comment(2)
Wow, thanks. I'm using git 2.7.4 and it is still not available there. It is awesome that this feature has native support now, I should update my client.Corri
FYI, it was specifically added in Git 2.11.Doubleminded
O
0

Git 2.22 (Q2 2019), git stash is rewritten in C.

See commit 7906af0, commit 90a4627, commit 8d8e9c2 (25 Feb 2019) by Johannes Schindelin (dscho).
See commit 40af146, commit 48ee24a, commit ef0f0b4, commit 64fe9c2, commit 1ac528c, commit d553f53, commit d4788af, commit 41e0dd5, commit dc7bd38, commit 130f269, commit bef55dc, commit dac566c, commit ab8ad46 (25 Feb 2019) by Paul-Sebastian Ungureanu (weekly-digest[bot]).
See commit c4de61d, commit 577c199, commit 4e2dd39, commit 8a0fc8d (25 Feb 2019) by Joel Teichroeb (klusark).
(Merged by Junio C Hamano -- gitster -- in commit e36adf7, 22 Apr 2019)


And... "git stash show 23" used to work, but no more after getting rewritten in C; this regression has been corrected in Git 2.23 (Q3 2019).

See commit 63b50c8 (15 Jun 2019) by Thomas Gummerer (tgummerer).
(Merged by Junio C Hamano -- gitster -- in commit 99af5be, 09 Jul 2019)

stash: fix show referencing stash index

In the conversion of 'stash show' to C in dc7bd38 ("stash: convert show to builtin", 2019-02-25, Git v2.22.0-rc0), 'git stash show <n>', where n is the index of a stash got broken, if n is not a file or a valid revision by itself.

'stash show' accepts any flag 'git diff' accepts for changing the output format.
Internally we use 'setup_revisions()' to parse these command line flags.
Currently we pass the whole argv through to 'setup_revisions()', which includes the stash index.

As the stash index is not a valid revision or a file in the working tree in most cases however, this 'setup_revisions()' call (and thus the whole command) ends up failing if we use this form of 'git stash show'.

Instead of passing the whole argv to 'setup_revisions()', only pass the flags (and the command name) through, while excluding the stash reference.
The stash reference is parsed (and validated) in 'get_stash_info()' already.

This separate parsing also means that we currently do produce the correct output if the command succeeds.

Orchestrate answered 14/7, 2019 at 18:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.