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


当前回答

如果你使用的是ZSH,这个别名组合非常致命:

zstyle ':completion:*' completer _expand_alias _complete _ignored
alias gs="git stash push -u -m "
alias gsp='git stash pop'

基本上,您可以使用tab自动完成别名,然后可以按名称轻松命名和搜索您的git存储。这个推送别名还将包括任何未跟踪的文件,我发现这些文件作为默认值很有用。

其他回答

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

不幸的是,git stash apply stash ^{/<regex>}不起作用(它实际上没有搜索存储列表,请参阅接受答案下的注释)。

下面是通过正则表达式搜索git stash列表以查找第一个(最近的)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=$1 | cut -f1 -d":") ; if [[ -n "$n" ]] ; then git stash show "$n" ; else echo "Error: No stash matches $1" ; return 1 ; fi }; f"
  sapply = "!f() { n=$(git stash list --max-count=1 --grep=$1 | cut -f1 -d":") ; if [[ -n "$n" ]] ; then git stash apply "$n" ; else echo "Error: No stash matches $1" ; 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")

请注意,将返回正确的结果代码,以便您可以在其他脚本中使用这些命令。使用以下命令运行命令后,可以验证这一点:

echo $?

请小心变量扩展漏洞,因为我不确定--grep=$1部分。它可能是--grep=“$1”,但我不确定这是否会干扰正则表达式分隔符(我愿意接受建议)。

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

所以,我不知道为什么在这个话题上会有如此多的恐慌。我可以用push和不推荐的save来命名git stash,也可以使用正则表达式通过apply将其拉回:

使用名称应用的Git存储方法

$ git stash push -m "john-hancock"

$ git stash apply stash^{/john-hancock}

如前所述,save命令已被弃用,但它仍然有效,因此您可以在无法通过推送调用更新它们的旧系统上使用它。与push命令不同,save不需要-m开关。

// save is deprecated but still functional  
$ git stash save john-hancock

这是Git 2.2和Windows 10。

视觉证明

这里有一个漂亮的动画GIF演示了这个过程。

事件顺序

GIF运行速度很快,但如果你看,过程如下:

ls命令显示目录中的4个文件touch example.html添加第五个文件git stash push-m“john hancock”-a(-a包括未跟踪的文件)ls命令在stash之后显示4个文件,这意味着stash和隐式硬重置工作正常git stash apply stash ^{/john hancock}运行ls命令列出了5个文件,显示example.html文件已返回,这意味着git stash apply命令有效。

这有道理吗?

坦率地说,我不确定这种方法的好处是什么。给储藏物起个名字是有价值的,但不包括检索。也许编写搁置和取消搁置过程的脚本会有所帮助,但只需按名称弹出一个存储库还是要容易得多。

$ git stash pop 3
$ git stash apply 3

这看起来比正则表达式容易得多。

使用git stash save NAME保存。

然后您可以使用此脚本选择应用(或弹出):

#!/usr/bin/env ruby
#git-stash-pick by Dan Rosenstark

# can take a command, default is apply
command = ARGV[0]
command = "apply" if !command
ARGV.clear

stashes = []
stashNames = []
`git stash list`.split("\n").each_with_index { |line, index|
    lineSplit = line.split(": ");
    puts "#{index+1}. #{lineSplit[2]}"
    stashes[index] = lineSplit[0]
    stashNames[index] = lineSplit[2]
}
print "Choose Stash or ENTER to exit: "
input = gets.chomp
if input.to_i.to_s == input
    realIndex = input.to_i - 1
    puts "\n\nDoing #{command} to #{stashNames[realIndex]}\n\n"
    puts `git stash #{command} #{stashes[realIndex]}`
end

我喜欢能够看到储藏物的名称并进行选择。此外,我使用Zshell,坦率地说,我不知道如何使用上面的一些Bash别名;)

注意:正如凯文所说,你应该使用标签和樱桃采摘代替。