在命令行上获取 git-commit-ids

Getting git-commit-ids on the commandline

当 运行 git commit --fixup=beefca7e 或在提交消息中引用以前的提交时,我不得不在笨拙的工作流程中使用鼠标。我使用 bash:

  1. 打开一个新终端tab/window/pane。
  2. git log --oneline --graph
  3. 扫描列表以找到相关的提交 sha。
  4. 鼠标,selectsha,复制到剪贴板。²
  5. 移回我工作的窗格并将其粘贴到那里。

这行得通。但我怀疑这可以更容易地完成。

是否有命令行工具、脚本或 git-addons 可以让我快速过滤提交并复制 selected 条目的 sha? 我的工作流程是错误的(或幼稚的)吗?我是否错过了某个重要的 git 功能?

能够在 vim 中使用它的好处,因为这是我编辑提交消息的编辑器。复制短 sha 而不是完整 sha 的奖励。


gitk/gitg 有针对 linux 的“快捷方式”:它们自动 select selected 提交的 sha1,将其放置在 X 剪贴板中,您可以使用“单击鼠标中键”粘贴 sha,无需任何其他操作。

在 windows 上,如果 sha1 是自动 selected,您可以直接 ctrl+C


从命令行:您可以使用 one of these tools,结合适当的命令,将 sha1 复制到剪贴板,但根据您的需要,可能会涉及“适当的命令”:

# easy :
git rev-parse HEAD~4 | xclip -selection c

# more involved :
clipsha () {
    sha1=$(git log --format="%H" --grep "");
    git log --oneline -1 $sha1;
    echo $sha1 | xclip -selection c;
    echo " *** copied sha1 to clipboard"
}
# usage :
clipsha "fixed issue #1234"  # will copy the sha1 of first which contains 
                             # the message 'fixed issue #1234'

如果您不想重新发明 tig,请检查我建议的重复问题中的
您可以在 tig 中添加快捷方式以将“selected 提交的 sha1”复制到剪贴板。


但说实话,我和你(或@torek)一样:我 copy/paste 通过 select 在我的终端上发送它们。

如果没有合适的工具就创建它)想出了这个:

#!/bin/bash
first_dialog() {
    dialog --output-fd 1        \
           --ok-label "Copy SHA" \
           --cancel-label "Exit"  \
           --menu "Select SHA to copy:" 0 0 0 "${list[@]}"
}

#-------------{ Create list for dialog }----------------
while read -r sha desc; do
    list+=( "$sha" "$desc" )
done < <(git log --oneline)

first_dialog

用法:

echo "test $(~/test) stst"
test 8cabb04 stst

或者像这样:

sha="$(~/ower/test)"
$ echo $sha
20799ef

转换为函数:

gsha() {
    list=()
    while read -r sha    desc; do
       list+=(  "$sha" "$desc" )
    done  < <(git log --oneline -n${1:-20})
    dialog --output-fd 1        \
           --ok-label "Copy SHA" \
           --cancel-label "Exit"  \
           --menu "Select SHA to copy:" 0 0 0 "${list[@]}"
}

sha="$(gsha)"
$ echo $sha
20799ef

我在 sshto project. Added this to my github also gsha

中使用了这种技术

尝试 the message-search syntax,你不必输入 sha,如果它是最近的提交,其消息提到 strcat 你可以说 :/strcat@^{/strcat}详细了解从何处开始在提交消息中查找 strcat

要获取哈希,请使用 rev-parse,如果您经常这样做,请创建一个 rp 别名或其他内容,然后 :r!git rp :/strcat 将 sha 放入您的编辑缓冲区。