Skip to content

Instantly share code, notes, and snippets.

@Ultrabenosaurus
Last active September 14, 2017 21:50
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ultrabenosaurus/6f499d25cb6d8f9e2256 to your computer and use it in GitHub Desktop.
Save Ultrabenosaurus/6f499d25cb6d8f9e2256 to your computer and use it in GitHub Desktop.
githash - get the short or full SHA for any commit in your current branch's history

githash

Easily get the short or full SHA for any commit in your current branch's history, useful for reverting and sharing specific code states with others.

Why?

Because typing out git rev-list --max-count=1 --abbrev-commit --skip=# HEAD takes far too much effort.

Installation

There are 3 different ways to install githash on your system:

  1. Copy githash.sh to your /git/bin.
  2. Alternatively, stick it anywhere on your system that is exposed in $PATH.
  3. You can put it somewhere outside of your $PATH, but then you'll have to type the filepath every time you want to use it...

Regardless of which you choose, it is recommend to drop the .sh file extension for ease of use - I only added it so the file would be recognised for syntax highlighting in this Gist.

Usage

Where # is the number of steps back through the branch's history to skip:

githash [full] [#]

Examples

The following examples assume you followed the first install method.

Latest commit's short hash:

latest commit's short hash

Latest commit's full hash:

latest commit's full hash

Short hash from the 6th previous commit:

short hash from the 6th previous commit

Full hash from the 6th previous commit:

full hash from the 6th previous commit

For comparison, here is the standard git rev-list output for the 7 most recent commits:

standard git rev-list output

COLOURS!!

Colours make things prettier. And stand out more.

If you don't like the ones I picked, though, I've defined the 3 I used as variables at the top of the script.

  • __short_color ~ the colour used to highlight the 7-char short hash.
  • __long_color ~ the colour used for the rest of the hash if you specify full.
  • __reset_color ~ restore the terminals default colours.

for more information on colouring your shell output, Google says "bash:tip_colors_and_formatting - FLOZz' MISC" is the best article.

To Do

  • Improve search
    • figure out how to suppress usage text from git branch on non-existant hash without also suppressing branch list on valid hash
    • show more details about commit if hash found
  • Try and tidy up the sed piping.
    • command substitution is evil and I hate it!

License

As usual with my work, this project is available under the BSD 3-Clause license. In short, you can do whatever you want with this code as long as:

  • I am always recognised as the original author.
  • I am not used to advertise any derivative works without prior permission.
  • You include a copy of said license and attribution with any and all redistributions of this code, including derivative works.

For more details, read about it on opensource.org.

#! /bin/bash
#
# githash
#
# @author Dan Bennett <http://about.me/d.bennett>
# @source https://gist.github.com/Ultrabenosaurus/6f499d25cb6d8f9e2256
# @license BSD 3-Clause <http://opensource.org/licenses/BSD-3-Clause>
#
__short_colour="\e[01;32m";
__long_colour="\e[1;37m";
__reset_colour="\e[m";
if [ $# -eq 0 ] ; then
__thing=`echo -e ${__short_colour}$(git rev-list --max-count=1 --abbrev-commit HEAD)`;
echo -e "${__thing}${__reset_colour}";
exit;
fi;
colour="";
#
# SEARCH STILL DOESN'T WORK PROPERLY
# COMMAND SUBSTITUTION IS EVIL AND I HATE IT
#
if [ "search" == $1 ] ; then
__search=`git branch --contains $2`;
echo -e $(echo "${__search}" | grep -o '[^\*]*\*[^\*]*' >> /dev/null && echo -e `git show-branch $2 | sed -r -e "s,(\[)([a-zA-Z0-9]+\])(.*),\\\\${__short_colour} \1\2\\\\${__long_colour}\3\\\\${__reset_colour},g"` || echo -e "${__long_colour} hash ${__short_colour}$2 ${__long_colour}not found in the current branch.${__reset_colour}");
exit;
fi;
if [ "full" == $1 ] ; then
scope=$2;
colour="colour";
else
scope=$1
fi;
if [ "colour" == "$colour" ] ; then
__hash=`git rev-list --max-count=1 --abbrev-commit --skip=${scope} HEAD`;
__pattern="s/(${__hash}).*/\1/g";
__thing=`echo -e ${__short_colour}$(git rev-list --max-count=1 --skip=${scope} HEAD | sed -r -e "${__pattern}")`;
__pattern="s/${__hash}(.*)/\1/g";
__thing+=`echo -e ${__long_colour}$(git rev-list --max-count=1 --skip=${scope} HEAD | sed -r -e "${__pattern}")`;
echo -e "${__thing}${__reset_colour}";
exit;
else
__thing=`echo -e ${__short_colour}$(git rev-list --max-count=1 --abbrev-commit --skip=${scope} HEAD)`;
echo -e "${__thing}${__reset_colour}";
exit;
fi;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment