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


当前回答

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

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

我希望这有帮助!

其他回答

要保存带有消息的存储:

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

我怀疑,如果你使用了太多的储藏物(比如说三个以上),那么你就做错了:Stashe通常用于中断工作,而不是实现功能(您可以使用功能分支来实现)。

假设您正在处理某个特性A,然后您发现必须解决的某个问题B(以实现特性A)。那么,您可以这样做:

gitadd——交互式修补特性A的部分,忽略问题B的修复。git将交互式选择提交到当前分支。git隐藏未提交的更改(修复问题B)返回主分支或主分支,可能检查新分支以解决问题B。git stash在当前分支中弹出问题B的修复程序并提交它们。如果存储需要手动合并,则可能会丢弃git存储。返回到特性A分支,并将其重新放置在具有问题B修复程序的分支上。然后,您就没有剩余的存储空间了,但在不同的分支上仍然具有特性A和问题B的修复程序。

您也可以先提交问题B的修复,然后隐藏特性A的更改,但您得到了这个想法。

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

这里是我的社区别名: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"

自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存储手册页