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?

  • Share pre-initialized GIT repository
  • Visual Studio 2010 git source control provider
  • Fatal: The remote end hung up unexpectedly while pushing to Git repository
  • git gui: how to clone and push?
  • How to get the current/active branch with LibGit2Sharp?
  • Partial squash-merge
  • Pod installation error: Unable to add a source with url `https://github.com/CocoaPods/Specs.git` named master
  • Can I setup Git to tell me each time a certain file(s) has been modified
  • What constitutes a merge conflict in Git?
  • How to avoid rebase hell when merge commits are in the way?
  • Make a shallow GIT repository less shallow
  • How to git diff multiple sibling git folders
  • 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.