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


当前回答

这里的聚会迟到了,但如果使用VSCode,一种快速的方法是打开命令调色板(CTRL/CMD+SHIFT+P)并键入“Pop-Stash”,您将能够按名称检索存储,而无需使用git CLI

其他回答

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

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

这个呢?

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

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

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

要保存带有消息的存储:

git stash push -m "my_stash_name"

或者(自v2.16起已弃用):

git stash save "my_stash_name"

要列出隐藏内容:

git stash list

所有的储藏物都存储在一个堆栈中。


要弹出(即应用并删除)第n个存储:

git stash pop stash@{n}

使用git stash pop无法按名称弹出(即应用和删除)存储(请参见脚注1)。


要应用第n个存储:

git stash apply stash@{n}

要按名称应用隐藏:

git stash apply stash^{/my_stash_name}

脚注1:

有关申请,请参阅man git stash部分:与pop不同,可以是任何看起来像是由stash push或stash create创建的提交。可能的解决方法(在git版本2.27和2.31上测试):git stash pop$(git stash-list--prey=“%gd%s”|grep“my_stash_name”|head-1|gawk“{print$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''
    }
}

此处显示更多详细信息