本文介绍了如何在git中通过名称命名和检索存储?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直觉得你可以通过做 git stash save stashname 给一个名字,你可以稍后通过做 git stash apply stashname 。但似乎在这种情况下,发生的所有情况是 stashname 将用作存储说明。



是有没有办法实际命名藏匿?如果不是,你会推荐什么来实现相同的功能?从本质上讲,我有一个小藏品,我会定期申请,但不希望总是在 git stash list 中搜索它的实际存储数量。

解决方案

您可以通过名称使用git的正则表达式语法来寻址对象:

 隐藏^ {/< regex>} 
:/< regex>

例如,当用保存名称保存储存时:

  git储存鳄梨酱在制品

...你可以使用正则表达式来解决这个问题:

  git stash apply stash ^ {/ guacamo} 

这将应用与正则表达式 guacamo 。这样,你不必知道堆栈里藏着什么数字,你只需要知道它的名字。这里没有一个简洁的语法,但是您可以在 .gitconfig 文件中创建一个别名:

  [别名] 
sshow =!f(){git stash show stash ^ {/ $ *} -p;}; f
sapply =!f() {git stash apply stash ^ {/ $ *};}; f

然后您可以使用 git sshow< regex> 显示:文件已更改,插入和删除

编辑:关于如何在git别名中使用bash参数。



编辑2:这个答案用于包含 drop list 别名,但我自此删除了它们,因为 drop 需要 stash @ {n} 语法,而 list 不会过滤在所有的藏品。如果有人知道如何解决隐藏的SHA-1哈希到存储引用,那么我也可以实现其他命令。



编辑3: strong>根据的建议我已经添加了一个修补程序标志来显示隐藏内容在显示时的内容。


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.

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.

解决方案

You can actually find the stash by name using git's regular expression syntax for addressing objects:

stash^{/<regex>}
:/<regex>

For example, when saving your stash with a save name:

git stash save "guacamole sauce WIP"

... you can use a regular expression to address that stash:

git stash apply stash^{/guacamo}

This will apply the youngest stash that matches the regular expression guacamo. That way, you don't have to know what number the stash is at in the stack, you just have to know its name. There is no terser syntax for this, but you can create an alias in your .gitconfig file:

[alias]
sshow = "!f() { git stash show stash^{/$*} -p; }; f"
sapply = "!f() { git stash apply stash^{/$*}; }; f"

You can then use git sapply <regex> to apply that stash (without dropping).
You can then use git sshow <regex> to show: files changed, insertions, and deletions

EDIT: Props to this StackOverflow answer on how to use bash arguments in git aliases.

EDIT 2: This answer used to contain drop and list aliases, but I've since removed them, since drop requires the stash@{n} syntax while list didn't filter the stashes at all. If anyone knows how to resolve a stash SHA-1 hash to a stash ref, then I could implement the other commands as well.

EDIT 3: Per isyi's suggestion I've added a patch flag to show what the contents of the stash are when showing one.

这篇关于如何在git中通过名称命名和检索存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 09:25