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:
git log --oneline --graph
.¹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.
git lg
.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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With