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


当前回答

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

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

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

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

其他回答

用名称保存一个git存储

$ git stash push -m "say-my-name"

按名称执行git存储应用

$ git stash apply stash^{/say-my-name}

如果您只是在寻找一种轻量级的方法来保存当前工作副本的部分或全部更改,然后稍后再重新应用它们,请考虑使用修补程序文件:

# save your working copy changes
git diff > some.patch

# re-apply it later
git apply some.patch

时不时地,我会想我是否应该为此使用储藏室,然后我会看到上面的疯狂,我对自己的所作所为感到满意:)

使用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别名;)

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

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

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

我希望这有帮助!

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