Reference git stash by number without stash@{i}

一笑奈何 提交于 2020-02-23 06:46:05

问题


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?


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/50611649/reference-git-stash-by-number-without-stashi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!