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


当前回答

这里有很多答案,但我相信OP想要的等效功能并没有被任何一个答案或评论完全封装。

通过将gitadd、gitdiff、gitrm和gitreset组合到一个自定义的git命令中,我们可以将更改快速聚合到一个补丁文件中,稍后我们可以通过名称轻松引用该文件:

以下是上述自定义git命令中使用的命令(也可用作要点)-请注意--hard标志的使用,它将重置当前分支,并删除对本地文件的所有更改:

#!/usr/bin/env bash

if [ $# -eq 1 ] ; then
  NAME=$1
else
  echo "Please pass exactly one argument, which is the name of the patch file"
  exit 1
fi

git add .

# if previous patch file with the same name exists untrack it
if [ -f "$NAME.patch" ] ; then
  git rm --cached $NAME.patch
fi

# warning: this will diff all changes into a file called NAME.patch and do a hard reset of the current branch

git diff --staged > $NAME.patch
git reset --hard $HEAD

现在,您可以简单地执行git-bottle hello来创建hello.patch文件。用gitapplyhello.patch应用它

诀窍是首先跟踪所有文件,以便我们可以利用diff命令的暂存(或缓存)选项。通过一些调整,您可以扩展自定义命令以将修补程序文件输出到工作目录之外的某个位置,例如,可能在硬盘上的某个修补程序文件夹中,或者您可以更新.gitignore文件以忽略它。

值得称赞的是:这个答案启发了我自己的想法,它描述了补丁方法,但忽略了新文件中的更改,将被排除在差异显示之外。

注意:由于此命令依赖于gitadd,因此不会将git已经忽略的任何文件中的更改打包。

其他回答

用名称保存一个git存储

$ git stash push -m "say-my-name"

按名称执行git存储应用

$ git stash apply stash^{/say-my-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

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

在我的鱼壳里

function gsap
  git stash list | grep ": $argv" | tr -dc '0-9' | xargs git stash apply
end

use

存储的gsap名称

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

此处显示更多详细信息

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

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