如何在 git 中按名称命名和检索存储?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11269256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to name and retrieve a stash by name in git?
提问by Suan
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
. But it seems that in this case all that happens is that stashname
will be used as the stash description.
我一直认为你可以通过 do 给 stash 取一个名字git stash save stashname
,然后你可以通过 do 来申请git stash apply stashname
。但在这种情况下,似乎所有发生的事情都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
其实际藏匿处的数量。
回答by Sri Murthy Upadhyayula
This is how you do it:
这是你如何做到的:
git stash save "my_stash"
Where "my_stash"
is the stash name.
"my_stash"
存储名称在哪里。
Some more useful things to know: All the stashes are stored in a stack. Type:
需要了解的一些更有用的事情:所有的 stash 都存储在一个堆栈中。类型:
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
隐藏更改的索引在哪里。
回答by EssaidiM
git stash save
is deprecatedas of 2.15.x/2.16, instead you can use git stash push -m "message"
git stash save
被弃用作为2.15.x / 2.16,而不是你可以使用git stash push -m "message"
You can use it like this:
你可以这样使用它:
git stash push -m "message"
git stash push -m "message"
where "message" is your note for that stash.
其中“消息”是您对该藏匿处的注释。
In order to retrieve the stash you can use: git stash list
. This will output a list like this, for example:
为了检索存储,您可以使用:git stash list
。这将输出一个这样的列表,例如:
stash@{0}: On develop: perf-spike
stash@{1}: On develop: node v10
Then you simply use apply
giving it the stash@{index}
:
然后你只需使用apply
给它stash@{index}
:
git stash apply stash@{1}
Referencesgit stash man page
回答by Adam Dymitruk
You can turn a stash into a branch if you feel it's important enough:
如果你觉得它足够重要,你可以把藏匿处变成一个分支:
git stash branch <branchname> [<stash>]
from the man page:
从手册页:
This creates and checks out a new branch named <branchname>
starting from the commit at which the <stash>
was originally created, applies the changes recorded in <stash>
to the new working tree and index, then drops the <stash>
if that completes successfully. When no <stash>
is given, applies the latest one.
这将创建并检出一个名为<branchname>
从<stash>
最初创建的提交开始的新分支,将记录的更改应用于<stash>
新的工作树和索引,然后删除<stash>
成功完成的if。如果没有<stash>
给出,则应用最新的。
This is useful if the branch on which you ran git stash save
has changed enough that git stash apply fails due to conflicts. 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 save
已更改到足以导致 git stash apply 由于冲突而失败,这将非常有用。由于 stash 应用在运行 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.
您稍后可以将此新分支重新定位到其他位置,该位置是您藏匿时所在位置的后代。
回答by Pat Niemeyer
If you are just looking for a lightweight way to save some or all of your current working copy changes and then reapply them later at will, consider a patch file:
如果您只是在寻找一种轻量级的方式来保存您当前的部分或全部工作副本更改,然后在以后随意重新应用它们,请考虑使用补丁文件:
# save your working copy changes
git diff > some.patch
# re-apply it later
git apply some.patch
Every now and then I wonder if I should be using stashes for this and then I see things like the insanity above and I'm content with what I'm doing :)
我时不时地想知道我是否应该为此使用 stash,然后我看到诸如上面的疯狂之类的东西,我对我正在做的事情感到满意:)
回答by Lily Ballard
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^
. Now when you want to reapply that stash you can use git cherry-pick -n tagname
(-n
is --no-commit
).
Stashes 并不意味着像你想要的那样是永久性的。在提交时使用标签可能会更好地为您服务。建造你想要藏起来的东西。做出承诺。为该提交创建一个标签。然后将您的分支回滚到HEAD^
. 现在,当您想重新应用该存储时,您可以使用git cherry-pick -n tagname
( -n
is --no-commit
)。
回答by canbax
use git stash push -m aNameForYourStash
to save it. Then use git stash list
to learn the index of the stashthat you want to apply. Then use git stash pop --index 0
to pop the stash and apply it.
使用git stash push -m aNameForYourStash
保存它。然后使用git stash list
来学习您要应用的 stash的索引。然后使用git stash pop --index 0
弹出藏匿处并应用它。
note: I'm using git version 2.21.0.windows.1
注意:我使用的是git 版本 2.21.0.windows.1
回答by iWheelBuy
I have these two functions in my .zshrc
file:
我的.zshrc
文件中有这两个函数:
function gitstash() {
git stash push -m "zsh_stash_name_"
}
function gitstashapply() {
git stash apply $(git stash list | grep "zsh_stash_name_" | cut -d: -f1)
}
Using them this way:
以这种方式使用它们:
gitstash nice
gitstashapply nice
回答by AdamB
What about this?
那这个呢?
git stash save stashname
git stash apply stash^{/stashname}
回答by Zack Morris
It's unfortunate that git stash apply stash^{/<regex>}
doesn't work (it doesn't actually search the stash list, see the comments under the accepted answer).
不幸的是,git stash apply stash^{/<regex>}
这不起作用(它实际上并没有搜索存储列表,请参阅已接受答案下的评论)。
Here are drop-in replacements that search git stash list
by regex to find the first (most recent) stash@{<n>}
and then pass that to git stash <command>
:
以下是git stash list
通过正则表达式搜索以查找第一个(最近的)stash@{<n>}
然后将其传递给的插入式替换git stash <command>
:
# standalone (replace <stash_name> with your regex)
(n=$(git stash list --max-count=1 --grep=<stash_name> | cut -f1 -d":") ; if [[ -n "$n" ]] ; then git stash show "$n" ; else echo "Error: No stash matches" ; return 1 ; fi)
(n=$(git stash list --max-count=1 --grep=<stash_name> | cut -f1 -d":") ; if [[ -n "$n" ]] ; then git stash apply "$n" ; else echo "Error: No stash matches" ; return 1 ; fi)
# ~/.gitconfig
[alias]
sshow = "!f() { n=$(git stash list --max-count=1 --grep= | cut -f1 -d":") ; if [[ -n "$n" ]] ; then git stash show "$n" ; else echo "Error: No stash matches " ; return 1 ; fi }; f"
sapply = "!f() { n=$(git stash list --max-count=1 --grep= | cut -f1 -d":") ; if [[ -n "$n" ]] ; then git stash apply "$n" ; else echo "Error: No stash matches " ; return 1 ; fi }; f"
# usage:
$ git sshow my_stash
myfile.txt | 1 +
1 file changed, 1 insertion(+)
$ git sapply my_stash
On branch master
Your branch is up to date with 'origin/master'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: myfile.txt
no changes added to commit (use "git add" and/or "git commit -a")
Note that proper result codes are returned so you can use these commands within other scripts. This can be verified after running commands with:
请注意,会返回正确的结果代码,因此您可以在其他脚本中使用这些命令。这可以在运行命令后验证:
echo $?
Just be careful about variable expansion exploitsbecause I wasn't sure about the --grep=$1
portion. It should maybe be --grep="$1"
but I'm not sure if that would interfere with regex delimiters (I'm open to suggestions).
请注意变量扩展漏洞,因为我不确定该--grep=$1
部分。也许应该是,--grep="$1"
但我不确定这是否会干扰正则表达式分隔符(我愿意接受建议)。
回答by Vlastimil Ov?á?ík
Alias
别名
sapply = "!f() { git stash apply \"$(git stash list | awk -F: --posix -vpat=\"$*\" \"$ 0 ~ pat {print $ 1; exit}\")\"; }; f"
sapply = "!f() { git stash apply \"$(git stash list | awk -F: --posix -vpat=\"$*\" \"$ 0 ~ pat {print $ 1; exit}\")\"; }; f"
Usage
用法
git sapply "<regex>"
git sapply "<regex>"
- compatible with Git for Windows
- 与适用于 Windows 的 Git 兼容
Edit: I sticked to my original solution, but I see why majority would prefer Etan Reisner's version (above). So just for the record:
编辑:我坚持我原来的解决方案,但我明白为什么大多数人更喜欢 Etan Reisner 的版本(上图)。所以只是为了记录:
sapply = "!f() { git stash apply \"$(git stash list | grep -E \"$*\" | awk \"{ print $ 1; }\" | sed -n \"s/://;1p\")\"; }; f"