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


当前回答

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

其他回答

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

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

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

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

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

我不认为有什么方法可以通过名字来获取一个隐藏的东西。

我已经创建了一个bash函数来实现它。

#!/bin/bash

function gstashpop {
  IFS="
"
  [ -z "$1" ] && { echo "provide a stash name"; return; }
  index=$(git stash list | grep -e ': '"$1"'$' | cut -f1 -d:)
  [ "" == "$index" ] && { echo "stash name $1 not found"; return; }
  git stash apply "$index"
}

用法示例:

[~/code/site] on master*
$ git stash push -m"here the stash name"
Saved working directory and index state On master: here the stash name

[~/code/site] on master
$ git stash list
stash@{0}: On master: here the stash name

[~/code/site] on master
$ gstashpop "here the 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"