git log abbreviated format length

I would like to get the SHA number from from GIT log in abbreviated format. This command will work:

git log -1 --format=%h

However, the default abbreviated format is 7 numbers in length.
Is there any way to change that?

  • Git & Android studio : Change the details of submitted change list
  • Git: Efficent way to generate a list of all files changed this year (on a current branch)
  • how can i ignore the .idea folder in .gitignore file?
  • Git merge between two folders, not branches
  • How to cache github repos so 2nd git clone will use the cache instead of github repo
  • Changing git authorship after git push
  • Git Revert or Reset on published Commits
  • How do you git fetch then merge? “Error: Your local changes to the following files would be overwritten by merge”
  • 2 Solutions collect form web for “git log abbreviated format length”

    For git log, the --abbrev=<length> parameter controls how long the output for %h and other abbreviated hashes is:

    $ git log -1 --format=%h --abbrev=4
    d157
    

    I will also note that when using -1 (or --no-walk which has the same effect in this particular case, but is more useful if you specify several commit identifiers), if all you want is the commit hash, using git log is overkill: git rev-parse will get you the hash. For no obvious reason, the control knob for limiting git rev-parse‘s commit IDs to a particular length is spelled --short rather than --abbrev; and git rev-parse requires that you spell out HEAD if you mean HEAD, so:

    $ git rev-parse --short=4 HEAD
    d157
    

    You can get the full hash with :

    git log -1 --format=%H
    

    You can also use an arbitrary number of chars with, for example for 6 digits:

    git log -1 --format=%h --abbrev=6
    

    Edit 1:

    To try how much saturated is the hash of the repo do the next:

    git rev-list --all --abbrev=0 --abbrev-commit |
        awk '{ a[length] += 1 } END { for (len in a) print len, a[len] }'
    

    I hope this helps 😀

    Git Baby is a git and github fan, let's start git clone.