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


当前回答

用名称保存一个git存储

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

按名称执行git存储应用

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

其他回答

不幸的是,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”,但我不确定这是否会干扰正则表达式分隔符(我愿意接受建议)。

这个答案在很大程度上归功于克莱曼·斯拉维奇。我本来会对接受的答案发表评论,但我还没有足够的代表:(

您还可以添加一个git别名来查找stash ref,并在其他别名中使用它来显示、应用、删除等。

[alias]
    sgrep = "!f() { ref=$(git --no-pager stash list | grep "$1" | cut -d: -f1 | head -n1); echo ${ref:-<no_match>}; }; f"
    sshow = "!f() { git stash show $(git sgrep "$1") -p; }; f"
    sapply = "!f() { git stash apply $(git sgrep "$1"); }; f"
    sdrop = "!f() { git stash drop $(git sgrep "$1"); }; f"

注意ref=$(…)的原因;echo${ref:-<no_match>};模式是这样的,不会返回空白字符串,这将导致sshow、sapply和sdrop以最新的存储为目标,而不是像预期的那样失败。

除了创建存储库之外,我还提出了另一种解决方案,即引入fzf作为依赖项。我建议你花5分钟的时间来了解它,因为它可以大大提高生产力。

总之,他们的示例页面中有一个相关的摘录,提供了隐藏搜索。很容易更改scriptlet以添加附加功能(如隐藏应用程序或删除):

fstash() {
    local out q k sha
    while out=$(
            git stash list --pretty="%C(yellow)%h %>(14)%Cgreen%cr %C(blue)%gs" |
            fzf --ansi --no-sort --query="$q" --print-query \
                --expect=ctrl-d,ctrl-b); do
        mapfile -t out <<< "$out"
        q="${out[0]}"
        k="${out[1]}"
        sha="${out[-1]}"
        sha="${sha%% *}"
        [[ -z "$sha" ]] && continue
        if [[ "$k" == 'ctrl-d' ]]; then
            git diff $sha
        elif [[ "$k" == 'ctrl-b' ]]; then
            git stash branch "stash-$sha" $sha
            break;
        else
            git stash show -p $sha
        fi
    done
}

别名

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"

这里有很多答案,但我相信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已经忽略的任何文件中的更改打包。