4

When running git commit --fixup=beefca7e or when referencing a previous commit in a commit message, I have to use the mouse in a clumsy workflow. I use bash:

  1. Open a new terminal tab/window/pane.
  2. git log --oneline --graph
  3. Scan through the list to find the relevant commit sha.
  4. Grab the mouse, select the sha, copy it to the clipboard.²
  5. Move back the the pane where I was working and paste it there.

This works. But I suspect this can be done much easier.

Are there commandline tools, scripts or git-addons out there that allow me to quickly filter through the commits and copy the sha of a selected entry? Is my workflow wrong(or naive), and did I miss an important git-feature somewhere?

Bonus for being able to use this in vim too, since that is my editor to edit commit messages. Bonus for copying the short sha instead of the full one.


  • ¹ I have a somewhat more complex alias for this named git lg.
  • ² xclip/gnome/clipboard manager is configured to auto-copy-on-select. Otherwise ctrl-c/cmd-c or so. Pasting is middle-mouse-button. Saves a few commands but still suboptimal. I'd rather not use the mouse at all and omit most steps.
berkes
  • 26,996
  • 27
  • 115
  • 206
  • 2
    You can use commit-relative syntax, but be sure you know if and when you might need a `^2` somewhere rather than just `HEAD~7`. Linear counting only works if you're on a linear piece of the commit graph. :-) You can also use commit message searching such as `:/foo`. In the end though I tend to just use cut and paste of the hash ID. – torek Sep 24 '20 at 09:04
  • Have you tried `tig`? – Ivan Sep 24 '20 at 09:09
  • @Ivan I have tried tig, I use `tig` and `gitg` mostly when I have to explore the history. Both `tig` and gitg, however, have no easy way to *copy the commit sha* AFAIK, which means I have to use tig or gitg over the common git-commands to commit, write messages, write fixups etc. Maybe I'll reconsider this if there's no easy way to "get a git sha" alltogheter. – berkes Sep 24 '20 at 09:22
  • Does this answer your question? [Take sha number of commit by tig](https://stackoverflow.com/questions/52542803/take-sha-number-of-commit-by-tig) – LeGEC Sep 24 '20 at 09:53

3 Answers3

3

If proper tool isn't there then create it) Came up with this:

#!/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

Usage:

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

Or like this:

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

Transformed to a function:

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

I've used this technique in my sshto project. Added this to my github also gsha

Ivan
  • 6,188
  • 1
  • 16
  • 23
2

gitk/gitg have "shortcuts" geared for linux : they auto select the sha1 of the selected commit, which places it in the X clipboard, you can the paste the sha with "middle click" without any other action.

On windows, if the sha1 is auto selected, you could just ctrl+C straight away.


From the command line : you could use one of these tools, combined with an adequate command, to copy a sha1 to the clipboard, but depending on your needs, the "adequate command" may become involved :

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

# more involved :
clipsha () {
    sha1=$(git log --format="%H" --grep "$1");
    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'

If you do not want to reinvent tig, check @VonC's answer in the question I suggested as duplicate :
you can add a shortcut in tig to copy "the sha1 of the selected commit" to the clipboard.


but truth be told, I do just like you (or @torek) do : I copy/paste sha1's by selecting them from my terminal.

LeGEC
  • 46,477
  • 5
  • 57
  • 104
0

Try the message-search syntax, you don't have to type the sha, if it's the most recent commit whose message mentions strcat you can say :/strcat or @^{/strcat} to get specific about where to start looking for strcat in the commit message.

To get the hash use rev-parse, if you do this a lot make an rp alias or something, then :r!git rp :/strcat to drop the sha in your edit buffer.

jthill
  • 55,082
  • 5
  • 77
  • 137