本文翻译自:How to name and retrieve a stash by name in git?
I was always under the impression that you could give a stash a name by doing git stash save stashname
, which you could later on apply by doing git stash apply stashname
. 我一直认为你可以通过git stash save stashname
来git stash save stashname
一个名字,你可以稍后通过git stash apply stashname
。 But it seems that in this case all that happens is that stashname
will be used as the stash description. 但似乎在这种情况下,所有发生的事情都是将stashname
用作存储描述。
Is there no way to actually name a stash? 有没有办法真正命名藏匿? If not, what would you recommend to achieve equivalent functionality? 如果没有,你会建议什么实现相同的功能? Essentially I have a small stash which I would periodically like to apply, but don't want to always have to hunt in git stash list
what its actual stash number is. 基本上我有一个小的藏匿,我会定期申请,但不想总是在git stash list
寻找它的实际藏匿号码。
参考:https://stackoom.com/question/lHeC/如何在git中按名称命名和检索存储
Stashes are not meant to be permanent things like you want. 被困不是你想要的永久性东西。 You'd probably be better served using tags on commits. 在提交时使用标签可能会更好。 Construct the thing you want to stash. 构建你想藏匿的东西。 Make a commit out of it. 做出承诺。 Create a tag for that commit. 为该提交创建标记。 Then roll back your branch to HEAD^
. 然后将您的分支回滚到HEAD^
。 Now when you want to reapply that stash you can use git cherry-pick -n tagname
( -n
is --no-commit
). 现在,当您想重新应用该存储时,您可以使用git cherry-pick -n tagname
( -n
是--no-commit
)。
You can turn a stash into a branch if you feel it's important enough: 如果你觉得它很重要,你可以把藏匿处变成一个分支:
git stash branch []
from the man page: 从手册页:
This creates and checks out a new branch named
starting from the commit at which the
was originally created, applies the changes recorded in
to the new working tree and index, then drops the
if that completes successfully. 这将创建并检查了一个名为新分支
从承诺开始在该
最初创建,应用记录在变化
新的工作树和索引,然后删除
如果成功完成。 When no
is given, applies the latest one. 如果没有给出
,则应用最新的。
This is useful if the branch on which you ran git stash save
has changed enough that git stash apply fails due to conflicts. 如果运行git stash save
的分支已经发生了足够的变化,git stash apply因冲突而失败,那么这很有用。 Since the stash is applied on top of the commit that was HEAD at the time git stash was run, it restores the originally stashed state with no conflicts. 由于存储是在运行git stash时的HEAD提交之上应用的,因此它会恢复原始存储状态而不会发生冲突。
You can later rebase this new branch to some other place that's a descendent of where you were when you stashed. 你可以稍后将这个新分支重新定位到其他地方,这个地方是你藏匿时所处的位置。
This is how you do it: 这是你如何做到的:
git stash save "my_stash"
Where "my_stash"
is the stash name. 其中"my_stash"
是"my_stash"
名称。
Some more useful things to know: All the stashes are stored in a stack. 一些更有用的知识:所有的存储都存储在一个堆栈中。 Type: 类型:
git stash list
This will list down all your stashes. 这将列出你的所有藏匿处。
To apply a stash and remove it from the stash stack, type: 要应用存储并将其从存储堆栈中删除,请键入:
git stash pop stash@{n}
To apply a stash and keep it in the stash stack, type: 要应用存储并将其保存在存储堆栈中,请键入:
git stash apply stash@{n}
Where n
is the index of the stashed change. 其中n
是隐藏变化的索引。
sapply = "!f() { git stash apply \\"$(git stash list | awk -F: --posix -vpat=\\"$*\\" \\"$ 0 ~ pat {print $ 1; exit}\\")\\"; }; f"
git sapply "
Edit: I sticked to my original solution, but I see why majority would prefer Etan Reisner's version (above). 编辑:我坚持我的原始解决方案,但我明白为什么大多数人会更喜欢Etan Reisner的版本(上图)。 So just for the record: 所以只是为了记录:
sapply = "!f() { git stash apply \"$(git stash list | grep -E \"$*\" | awk \"{ print $ 1; }\" | sed -n \"s/://;1p\")\"; }; f"
Alias This might be a more direct syntax for Unix-like systems without needing to encapsulate in a function. 别名这可能是类Unix系统的更直接的语法,无需封装在函数中。 Add the following to ~/.gitconfig under [alias] 将以下内容添加到[alias]下的〜/ .gitconfig
sshow = !sh -c 'git stash show stash^{/$*} -p' -
sapply = !sh -c 'git stash apply stash^{/$*}' -
ssave = !sh -c 'git stash save "${1}"' -
Usage: sapply regex 用法:sapply 正则表达式
Example: git sshow MySecretStash 示例:git sshow MySecretStash
The hyphen at the end says take input from standard input. 最后的连字符表示从标准输入中获取输入。