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.
Because typing out git rev-list --max-count=1 --abbrev-commit --skip=# HEAD
takes far too much effort.
There are 3 different ways to install githash on your system:
- Copy
githash.sh
to your/git/bin
. - Alternatively, stick it anywhere on your system that is exposed in
$PATH
. - 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.
Where #
is the number of steps back through the branch's history to skip:
githash [full] [#]
The following examples assume you followed the first install method.
Latest commit's short hash:
Latest commit's full hash:
Short 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:
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 specifyfull
.__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.
- 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
- figure out how to suppress usage text from
- Try and tidy up the
sed
piping.- command substitution is evil and I hate it!
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.