I often need to get the current SHA1 after making a new commit, so I can paste it into a
cherry-pick on a different branch, or into the deploy interface. The easiest way is by adding an alias:
git config --global alias.sha "!git log -n 1 --pretty=format:"%H" | pbcopy"
A git alias is usually just a regular git command- meaning you cannnot use pipes or redirection- but starting it with the bang (!) lets you fork a shell to run the commands in. Here, I’m piping the output of
git log -n 1 --pretty=format:"%H" (which outputs the SHA1 of the current tree) to
pbcopy, an OS X utility that copies its input to the clipboard. I’m told that
xclip -selection clipboard does the same on Ubuntu.
Now, simply type in
git sha after making or checking out a commit, and you’ll have it’s hash copied to your clipboard.