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


当前回答

所以,我不知道为什么在这个话题上会有如此多的恐慌。我可以用push和不推荐的save来命名git stash,也可以使用正则表达式通过apply将其拉回:

使用名称应用的Git存储方法

$ git stash push -m "john-hancock"

$ git stash apply stash^{/john-hancock}

如前所述,save命令已被弃用,但它仍然有效,因此您可以在无法通过推送调用更新它们的旧系统上使用它。与push命令不同,save不需要-m开关。

// save is deprecated but still functional  
$ git stash save john-hancock

这是Git 2.2和Windows 10。

视觉证明

这里有一个漂亮的动画GIF演示了这个过程。

事件顺序

GIF运行速度很快,但如果你看,过程如下:

ls命令显示目录中的4个文件touch example.html添加第五个文件git stash push-m“john hancock”-a(-a包括未跟踪的文件)ls命令在stash之后显示4个文件,这意味着stash和隐式硬重置工作正常git stash apply stash ^{/john hancock}运行ls命令列出了5个文件,显示example.html文件已返回,这意味着git stash apply命令有效。

这有道理吗?

坦率地说,我不确定这种方法的好处是什么。给储藏物起个名字是有价值的,但不包括检索。也许编写搁置和取消搁置过程的脚本会有所帮助,但只需按名称弹出一个存储库还是要容易得多。

$ git stash pop 3
$ git stash apply 3

这看起来比正则表达式容易得多。

其他回答

这是使用PowerShell实现此目的的一种方法:

<#
.SYNOPSIS
Restores (applies) a previously saved stash based on full or partial stash name.

.DESCRIPTION
Restores (applies) a previously saved stash based on full or partial stash name and then optionally drops the stash. Can be used regardless of whether "git stash save" was done or just "git stash". If no stash matches a message is given. If multiple stashes match a message is given along with matching stash info.

.PARAMETER message
A full or partial stash message name (see right side output of "git stash list"). Can also be "@stash{N}" where N is 0 based stash index.

.PARAMETER drop
If -drop is specified, the matching stash is dropped after being applied.

.EXAMPLE
Restore-Stash "Readme change"
Apply-Stash MyStashName
Apply-Stash MyStashName -drop
Apply-Stash "stash@{0}"
#>
function Restore-Stash  {
    [CmdletBinding()]
    [Alias("Apply-Stash")]
    PARAM (
        [Parameter(Mandatory=$true)] $message,         
        [switch]$drop
    )

    $stashId = $null

    if ($message -match "stash@{") {
        $stashId = $message
    }

    if (!$stashId) {
        $matches = git stash list | Where-Object { $_ -match $message }

        if (!$matches) {
            Write-Warning "No stashes found with message matching '$message' - check git stash list"
            return
        }

        if ($matches.Count -gt 1) {
            Write-Warning "Found $($matches.Count) matches for '$message'. Refine message or pass 'stash{@N}' to this function or git stash apply"
            return $matches
        }

        $parts = $matches -split ':'
        $stashId = $parts[0]
    }

    git stash apply ''$stashId''

    if ($drop) {
        git stash drop ''$stashId''
    }
}

此处显示更多详细信息

除了创建存储库之外,我还提出了另一种解决方案,即引入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
}

Stashes并不是你想要的永久性的东西。在提交时使用标记可能会更好。构造你想要隐藏的东西。从中进行提交。为该提交创建标记。然后将你的分支回滚到HEAD^。现在,当您想重新应用该存储时,可以使用gitcherry-pick-n标记名(-n是--no commit)。

别名

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"

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

您还可以添加一个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以最新的存储为目标,而不是像预期的那样失败。