Use the same commit abbreviation length for git blame as other commands

Viewed 138

The git blame command shows commit hashes abbreviated to a length that is one character longer than other commands. For example:

$ git log --oneline       
9fb6f706 (HEAD -> master) second commit
0af747a8 first commit
$ git blame foo   
9fb6f7064 (gilles 2020-11-15 12:28:09 +0100 1) revised
^0af747a8 (gilles 2020-11-15 12:27:41 +0100 2) world

I frequently copy-paste an abbreviated hash from the blame output and search for it in logs or in the set of commits in an interactive rebase. But because the abbreviation is one character longer in the git blame output, I have to remember to delete the last character, otherwise the search can't find anything.

For scripting I'd use unabbreviated hashes and porcelain formats. But for interactive use, I want to use abbreviated hashes.

Setting the core.abbrev option doesn't help: git blame adds one to that. Setting core.abbrev and calling blame --abbrev with a value that's one less works, but is not a good solution because I lose the benefit of git's heuristics to determine a good length for short commit ids, and I have to pass this option explicitly or use a different command name as an alias.

How can I make a plain git blame use the same length for abbreviated commit ids as other git commands?

1 Answers

Thanks for digging through the source code. I'm afraid your only solution – rather workaround is to define a custom alias.

git config --global alias.blame2 '!git -c core.abbrev=6 blame'
Related