git_get_short_hash

This script will return the short hash of the HEAD commit of the checked out branch of the given Git repository directory ($PWD if not given).

The suffix -dirty will be appended if the working tree is dirty.

The length of the hash can be configured via the optional second parameter (range: [4, 40] for SHA-1 object names or [4, 64] for SHA-256 object names); the default is determined by the core.abbrev Git configuration variable.

To get a consistent hash length across systems you should either

  1. ensure that core.abbrev is set on the repository after initialization:

    $ git config --local core.abbrev 20

    Unfortunately, these settings are not under version control.

  2. explicitly set the length when invoking the script:

    $ scripts/git/git_get_short_hash.sh . 20

Usage

$ scripts/git/git_get_short_hash.sh
844881d
$ scripts/git/git_get_short_hash.sh path/to/git/repository
dbd0ffb
$ scripts/git/git_get_short_hash.sh . 10
844881d148
$ git config --local core.abbrev 20
$ scripts/git/git_get_short_hash.sh
844881d148be35d7c0a9
$ touch a
$ scripts/git/git_get_short_hash.sh
844881d-dirty
  • git_get_hash

    $ scripts/git/git_get_short_hash.sh
    844881d
    $ scripts/git/git_get_hash.sh
    844881d148be35d7c0a9bcbf5ba23ab79cf14c6e