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


当前回答

除了创建存储库之外,我还提出了另一种解决方案,即引入fzf作为依赖项。我建议你花5分钟的时间来了解它,因为它可以大大提高生产力。

总之,他们的示例页面中有一个相关的摘录,提供了隐藏搜索。很容易更改scriptlet以添加附加功能(如隐藏应用程序或删除):

fstash() {
    local out q k sha
    while out=$(
            git stash list --pretty="%C(yellow)%h %>(14)%Cgreen%cr %C(blue)%gs" |
            fzf --ansi --no-sort --query="$q" --print-query \
                --expect=ctrl-d,ctrl-b); do
        mapfile -t out <<< "$out"
        q="${out[0]}"
        k="${out[1]}"
        sha="${out[-1]}"
        sha="${sha%% *}"
        [[ -z "$sha" ]] && continue
        if [[ "$k" == 'ctrl-d' ]]; then
            git diff $sha
        elif [[ "$k" == 'ctrl-b' ]]; then
            git stash branch "stash-$sha" $sha
            break;
        else
            git stash show -p $sha
        fi
    done
}

其他回答

我怀疑,如果你使用了太多的储藏物(比如说三个以上),那么你就做错了:Stashe通常用于中断工作,而不是实现功能(您可以使用功能分支来实现)。

假设您正在处理某个特性A,然后您发现必须解决的某个问题B(以实现特性A)。那么,您可以这样做:

gitadd——交互式修补特性A的部分,忽略问题B的修复。git将交互式选择提交到当前分支。git隐藏未提交的更改(修复问题B)返回主分支或主分支,可能检查新分支以解决问题B。git stash在当前分支中弹出问题B的修复程序并提交它们。如果存储需要手动合并,则可能会丢弃git存储。返回到特性A分支,并将其重新放置在具有问题B修复程序的分支上。然后,您就没有剩余的存储空间了,但在不同的分支上仍然具有特性A和问题B的修复程序。

您也可以先提交问题B的修复,然后隐藏特性A的更改,但您得到了这个想法。

这个呢?

git stash save stashname
git stash apply stash^{/stashname}

我的.zshrc文件中有以下两个函数:

function gitstash() {
    git stash push -m "zsh_stash_name_$1"
}

function gitstashapply() {
    git stash apply $(git stash list | grep "zsh_stash_name_$1" | cut -d: -f1)
}

使用方法如下:

gitstash nice
gitstashapply nice

使用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别名;)

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

别名

sapply=“!f(){git stash apply\”$(git stash-list|awk-f:--posix-vpat=\“$*\”\“$0~pat{print$1;exit}\”)\“;};f”

用法

git sapply“<regex>”

与Git for Windows兼容

编辑:我坚持我最初的解决方案,但我明白了为什么大多数人更喜欢埃坦·雷斯纳的版本(上图)。所以,为了记录在案:

sapply = "!f() { git stash apply \"$(git stash list | grep -E \"$*\" | awk \"{ print $ 1; }\" | sed -n \"s/://;1p\")\"; }; f"