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


当前回答

别名

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"

其他回答

这是使用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
}

我的.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 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以最新的存储为目标,而不是像预期的那样失败。

别名对于类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

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