Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting git-commit-ids on the commandline

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.
like image 855
berkes Avatar asked Oct 18 '25 14:10

berkes


2 Answers

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

like image 140
Ivan Avatar answered Oct 20 '25 04:10

Ivan


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.

like image 24
LeGEC Avatar answered Oct 20 '25 04:10

LeGEC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!