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


当前回答

如果您只是在寻找一种轻量级的方法来保存当前工作副本的部分或全部更改,然后稍后再重新应用它们,请考虑使用修补程序文件:

# save your working copy changes
git diff > some.patch

# re-apply it later
git apply some.patch

时不时地,我会想我是否应该为此使用储藏室,然后我会看到上面的疯狂,我对自己的所作所为感到满意:)

其他回答

Stash可以使用以下命令自定义注释。

PS D:\git-example> git stash -m "your comment"

列出藏匿物品

PS D:\git-exapmle> git stash list

stash@{0}: On master: first stash
stash@{1}: On master: second stash

我们可以选择任何一个储藏物,我们必须传递储藏物@{x},下面我选择第二个储藏物是1。

PS D:\git-example> git stash pop 1

使用git stash push-m aNameForYourStash来保存它。然后使用git stash list来了解要应用的存储的索引。然后使用git stash pop--index 0弹出存储并应用它。

注意:我使用的是git版本2.21.0.windows.1

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

此处显示更多详细信息

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

我已经创建了一个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"

我希望这有帮助!

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

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