Curtis Free's Blog

About Blog Config DB Projects Scripts Wishlist Husband, father, geek, progressive.

Quickly copy last Git commit hash

A typical portion of my Git workflow involves the following:

  1. Local git commit
  2. git push to upstream remote
  3. Add a comment to an issue tracker that includes the new commit hash

A simple shell (ZSH, BASH, or similar) function makes the last step a little bit easier:

gitcp() { git log -1 --format="%H" | xclip -in; }

After running gitcp, the hash of the last commit is in X’s primary selection and can easily be pasted into a comment box in the issue tracker.

Depending on your preferences, you can instruct xclip to copy into a different X “selection” – or you can even use Autocutsel to sync your X selections.

Not using X11? There are xclip alternatives for OS X and Windows, so you can spin up your own helper utility.