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


当前回答

要保存带有消息的存储:

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}”)

其他回答

在我的鱼壳里

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

use

存储的gsap名称

这个答案在很大程度上归功于克莱曼·斯拉维奇。我本来会对接受的答案发表评论,但我还没有足够的代表:(

您还可以添加一个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以最新的存储为目标,而不是像预期的那样失败。

这个呢?

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

这里有很多答案,但我相信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}