Snippets

Radistao A Copy git HEAD commit hash to clipboard

Created by Radistao A last modified
sudo sh -c $'( cd $(git --exec-path) && 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
)' && git config --global alias.ch copy-head && printf "\n\e[1;32mSUCCESS!\e[0m Try now:\n  \033[1mgit copy-head\e[0m\nor\n  \033[1mgit ch\e[0m"

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

  1. Go to git scripts directory:

    pushd $(git --exec-path)

  2. 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

  3. Now you could easily do:

    git copy-head

    and try Ctrl+V

  4. 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

1
2
3
4
5
6
7
8
9
#!/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

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.