参考 git 按数字存储而不使用 stash@{i}

Reference git stash by number without stash@{i}

我正在大量使用 git 藏品。有时输入 stash@{3} 会很烦人,乍一看,一个简单的 3 就足够了。是否可以以某种方式使用较短的参考文献?

我知道一个使用 shell 别名的解决方案。制作一个像

这样的别名
sshow = "!f { git stash show @{$@}; }; f"

允许使用 git sshow 1。如果 $@ 被正确扩展,它甚至允许将额外的参数传递给 git stash show

然而,在这种情况下,git 自动完成不起作用:当输入 git stash show stash@{0} --<Tab><Tab> 时,它应该显示所有 git diff 选项,但在这里它对底层命令一无所知。

是否有其他方法可以制作保留原始 git diff 完成上下文的别名?

是的,从 v2.11 开始,这是 Git 的内置功能。您只能按索引引用任何存储。例如,要在索引 2 处应用存储,您可以键入

git stash apply 2

Git 2.22(2019 年第 2 季度),git stash 用 C 重写。

参见 commit 7906af0, commit 90a4627, commit 8d8e9c2 (25 Feb 2019) by Johannes Schindelin (dscho)
参见 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])
参见 commit c4de61d, commit 577c199, commit 4e2dd39, commit 8a0fc8d (25 Feb 2019) by Joel Teichroeb (klusark)
(由 Junio C Hamano -- gitster -- in commit e36adf7 合并,2019 年 4 月 22 日)


并且...“git stash show 23曾经可以工作,但在用 C 语言重写后就不行了;此回归已在 Git 2.23(2019 年第三季度)中得到纠正。

参见 commit 63b50c8 (15 Jun 2019) by Thomas Gummerer (tgummerer)
(由 Junio C Hamano -- gitster -- in commit 99af5be 合并,2019 年 7 月 9 日)

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.