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


当前回答

这里是我的社区别名:wip和wip适用。当您获取wip时,您还存储了未跟踪的文件,并返回到以前的提交状态。

git config --global alias.wip '!f() { git stash save $1 -u ; }; f'       

git config --global alias.wip-apply '!f() { temp=$(git stash list | cut -d ':' -f 3 | grep -n -w $1 | cut -d ':' -f 1) ; stashnum=$((temp-1)) ; stashname=$(echo stash@{$stashnum}) ; git stash apply $stashname ; }; f'

用法:

git wip "featureA"
git wip-apply "featureA"

其他回答

别名

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"

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

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

自2.15.x/2.16起,git stash save已被弃用,您可以使用git stass push-m“message”

您可以这样使用:

git stash push-m“消息”

其中“message”是你的留言。

为了检索存储,您可以使用:git存储列表。这将输出如下列表,例如:

stash@{0}: On develop: perf-spike
stash@{1}: On develop: node v10

然后,只需使用apply为其提供存储@{index}:

git stash apply 1

工具书类git存储手册页

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

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

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

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