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


当前回答

这里有很多答案,但我相信OP想要的等效功能并没有被任何一个答案或评论完全封装。

通过将gitadd、gitdiff、gitrm和gitreset组合到一个自定义的git命令中,我们可以将更改快速聚合到一个补丁文件中,稍后我们可以通过名称轻松引用该文件:

以下是上述自定义git命令中使用的命令(也可用作要点)-请注意--hard标志的使用,它将重置当前分支,并删除对本地文件的所有更改:

#!/usr/bin/env bash

if [ $# -eq 1 ] ; then
  NAME=$1
else
  echo "Please pass exactly one argument, which is the name of the patch file"
  exit 1
fi

git add .

# if previous patch file with the same name exists untrack it
if [ -f "$NAME.patch" ] ; then
  git rm --cached $NAME.patch
fi

# warning: this will diff all changes into a file called NAME.patch and do a hard reset of the current branch

git diff --staged > $NAME.patch
git reset --hard $HEAD

现在,您可以简单地执行git-bottle hello来创建hello.patch文件。用gitapplyhello.patch应用它

诀窍是首先跟踪所有文件,以便我们可以利用diff命令的暂存(或缓存)选项。通过一些调整,您可以扩展自定义命令以将修补程序文件输出到工作目录之外的某个位置,例如,可能在硬盘上的某个修补程序文件夹中,或者您可以更新.gitignore文件以忽略它。

值得称赞的是:这个答案启发了我自己的想法,它描述了补丁方法,但忽略了新文件中的更改,将被排除在差异显示之外。

注意:由于此命令依赖于gitadd,因此不会将git已经忽略的任何文件中的更改打包。

其他回答

别名对于类Unix系统,这可能是一种更直接的语法,而不需要封装在函数中。将以下内容添加到[别名]下的~/.gitconfig

sshow = !sh -c 'git stash show stash^{/$*} -p' -
sapply = !sh -c 'git stash apply stash^{/$*}' -
ssave = !sh -c 'git stash save "${1}"' -

用法:蓝宝石正则表达式

例子:git-show MySecretStash

结尾的连字符表示从标准输入中获取输入。

我的.zshrc文件中有以下两个函数:

function gitstash() {
    git stash push -m "zsh_stash_name_$1"
}

function gitstashapply() {
    git stash apply $(git stash list | grep "zsh_stash_name_$1" | cut -d: -f1)
}

使用方法如下:

gitstash nice
gitstashapply nice

Stash可以使用以下命令自定义注释。

PS D:\git-example> git stash -m "your comment"

列出藏匿物品

PS D:\git-exapmle> git stash list

stash@{0}: On master: first stash
stash@{1}: On master: second stash

我们可以选择任何一个储藏物,我们必须传递储藏物@{x},下面我选择第二个储藏物是1。

PS D:\git-example> git stash pop 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存储手册页

Stashes并不是你想要的永久性的东西。在提交时使用标记可能会更好。构造你想要隐藏的东西。从中进行提交。为该提交创建标记。然后将你的分支回滚到HEAD^。现在,当您想重新应用该存储时,可以使用gitcherry-pick-n标记名(-n是--no commit)。