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


当前回答

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

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

我希望这有帮助!

其他回答

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

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

我希望这有帮助!

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

别名

sapply=“!f(){git stash apply\”$(git stash-list|awk-f:--posix-vpat=\“$*\”\“$0~pat{print$1;exit}\”)\“;};f”

用法

git sapply“<regex>”

与Git for Windows兼容

编辑:我坚持我最初的解决方案,但我明白了为什么大多数人更喜欢埃坦·雷斯纳的版本(上图)。所以,为了记录在案:

sapply = "!f() { git stash apply \"$(git stash list | grep -E \"$*\" | awk \"{ print $ 1; }\" | sed -n \"s/://;1p\")\"; }; f"

我的.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

gitstash-apply还可以与stash@{0}以外的其他ref一起使用。因此,您可以使用普通标记来获取持久名称。这还有一个好处,那就是你不会不小心把git藏起来或者把git隐藏起来。

因此,您可以这样定义别名pstash(又名“持久存储”):

git config --global alias.pstash '!f(){ git stash && git tag "$1" stash && git stash drop; }; f'

现在,您可以创建标记的存储:

git pstash x-important-stuff

并照常显示和应用:

git stash show x-important-stuff
git stash apply x-important-stuff