Snippets
How to copy easily git HEAD commit to clipboard
Very often when addressing/fixing PR review comments you make a commit and then you need this commit hash to past into GH comment.
This is the way how to automate it:
Using bash alias
alias gitch='COMM=`git rev-parse --short=7 HEAD` && echo "$COMM" | xclip -selection clipboard && printf "%nCopied HEAD commit ${COMM} to clipboard%n"'
Using custom git commands
-
Go to git scripts directory:
pushd $(git --exec-path)
-
Create executable custom script git-copy-head:
touch git-copy-head ; chmod a+x git-copy-head ; cat <<EOT >> git-copy-head
#!/bin/sh
# radistao's custom script to copy head commit hash to clipboard
# xargs echo -n trims last new line from rev-parse
HASH=`git rev-parse --short HEAD | xargs echo -n`
printf "$HASH" | xclip -selection clipboard
printf "Copied HEAD hash \e[1;32m\$HASH\e[0m to clipboard:\n\n"
git --no-pager show -s --pretty=short HEAD
EOT
-
Now you could easily do:
git copy-head
and try
Ctrl+V
-
To simplify even more - add git alias:
git config --global alias.ch copy-head
now you could just simple:
git ch
and try
Ctrl+V
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.