如何保存/应用带有名称的存储?我不想在git存储列表中查找它的索引号。我尝试了git存储保存“my_stash_name”,但这只会更改存储描述,相应的git-apply“my_stash_name”不起作用。


当前回答

别名对于类Unix系统,这可能是一种更直接的语法,而不需要封装在函数中。将以下内容添加到[别名]下的~/.gitconfig

sshow = !sh -c 'git stash show stash^{/$*} -p' -
sapply = !sh -c 'git stash apply stash^{/$*}' -
ssave = !sh -c 'git stash save "${1}"' -

用法:蓝宝石正则表达式

例子:git-show MySecretStash

结尾的连字符表示从标准输入中获取输入。

其他回答

这个答案在很大程度上归功于克莱曼·斯拉维奇。我本来会对接受的答案发表评论,但我还没有足够的代表:(

您还可以添加一个git别名来查找stash ref,并在其他别名中使用它来显示、应用、删除等。

[alias]
    sgrep = "!f() { ref=$(git --no-pager stash list | grep "$1" | cut -d: -f1 | head -n1); echo ${ref:-<no_match>}; }; f"
    sshow = "!f() { git stash show $(git sgrep "$1") -p; }; f"
    sapply = "!f() { git stash apply $(git sgrep "$1"); }; f"
    sdrop = "!f() { git stash drop $(git sgrep "$1"); }; f"

注意ref=$(…)的原因;echo${ref:-<no_match>};模式是这样的,不会返回空白字符串,这将导致sshow、sapply和sdrop以最新的存储为目标,而不是像预期的那样失败。

使用git stash save NAME保存。

然后您可以使用此脚本选择应用(或弹出):

#!/usr/bin/env ruby
#git-stash-pick by Dan Rosenstark

# can take a command, default is apply
command = ARGV[0]
command = "apply" if !command
ARGV.clear

stashes = []
stashNames = []
`git stash list`.split("\n").each_with_index { |line, index|
    lineSplit = line.split(": ");
    puts "#{index+1}. #{lineSplit[2]}"
    stashes[index] = lineSplit[0]
    stashNames[index] = lineSplit[2]
}
print "Choose Stash or ENTER to exit: "
input = gets.chomp
if input.to_i.to_s == input
    realIndex = input.to_i - 1
    puts "\n\nDoing #{command} to #{stashNames[realIndex]}\n\n"
    puts `git stash #{command} #{stashes[realIndex]}`
end

我喜欢能够看到储藏物的名称并进行选择。此外,我使用Zshell,坦率地说,我不知道如何使用上面的一些Bash别名;)

注意:正如凯文所说,你应该使用标签和樱桃采摘代替。

这里的聚会迟到了,但如果使用VSCode,一种快速的方法是打开命令调色板(CTRL/CMD+SHIFT+P)并键入“Pop-Stash”,您将能够按名称检索存储,而无需使用git CLI

不幸的是,git stash apply stash ^{/<regex>}不起作用(它实际上没有搜索存储列表,请参阅接受答案下的注释)。

下面是通过正则表达式搜索git stash列表以查找第一个(最近的)stash@{<n>},然后将其传递给git stash<command>的替换项:

# standalone (replace <stash_name> with your regex)
(n=$(git stash list --max-count=1 --grep=<stash_name> | cut -f1 -d":") ; if [[ -n "$n" ]] ; then git stash show "$n" ; else echo "Error: No stash matches" ; return 1 ; fi)
(n=$(git stash list --max-count=1 --grep=<stash_name> | cut -f1 -d":") ; if [[ -n "$n" ]] ; then git stash apply "$n" ; else echo "Error: No stash matches" ; return 1 ; fi)
# ~/.gitconfig
[alias]
  sshow = "!f() { n=$(git stash list --max-count=1 --grep=$1 | cut -f1 -d":") ; if [[ -n "$n" ]] ; then git stash show "$n" ; else echo "Error: No stash matches $1" ; return 1 ; fi }; f"
  sapply = "!f() { n=$(git stash list --max-count=1 --grep=$1 | cut -f1 -d":") ; if [[ -n "$n" ]] ; then git stash apply "$n" ; else echo "Error: No stash matches $1" ; return 1 ; fi }; f"

# usage:

$ git sshow my_stash
 myfile.txt | 1 +
 1 file changed, 1 insertion(+)

$ git sapply my_stash
On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   myfile.txt

no changes added to commit (use "git add" and/or "git commit -a")

请注意,将返回正确的结果代码,以便您可以在其他脚本中使用这些命令。使用以下命令运行命令后,可以验证这一点:

echo $?

请小心变量扩展漏洞,因为我不确定--grep=$1部分。它可能是--grep=“$1”,但我不确定这是否会干扰正则表达式分隔符(我愿意接受建议)。

使用一个小型bash脚本来查找存储的数量。称之为“gitapply”:

NAME="$1"
if [[ -z "$NAME" ]]; then echo "usage: gitapply [name]"; exit; fi
git stash apply $(git stash list | grep "$NAME" | cut -d: -f1)

用法:

gitapply foo

…其中foo是所需存储的名称的子字符串。