Why does the reduced version of git SHA1 code have 7 characters by default?

Asked

Viewed 618 times

8

I’m studying Git and came across the command cherry-pick, who recovers a commit specific. The parameter passed for this command is the code hash generated to identify only that commit. However, in the examples of the documentation, a reduced version of this hash, with only 7 characters.

Examples:

git cherry-pick ae33630

This example above is the reduced version of the full hash, which would be: ae33630626ac157ea7573233114b560d93f509e0

So I’d like to know why Git accept a reduced version of hash and why specifically be 7 characters.

In addition, another question arises: being a reduced version, there could be no collisions of hashs different characters starting with the same characters?

1 answer

9


Git contains a plethora of day-to-day commands and workflows that you use to manage or maintain source control of a Git repository. With such command you will be able to perform basic tasks of tracking and compromising files.

Beyond all this Git contains tools for review selection, where you will be able to explore a number of very powerful things that Git can do. They are not tools that you will necessarily use on a day-to-day basis, but you may need at some point. And one of them is Short SHA-1.

Rephrase the question: There could be different hash collisions that start with the same characters?

Git is smart enough to figure out what you want to type if you provide the first characters of hash, since his SHA-1 partial has at least four characters and is not ambiguous, that is, only one hash in the current repository starting with the SHA-1 partial.

Git can find a short and unique abbreviation for its values SHA-1 through the command --abbrev-commit for the command git log. The exit will be hash shorter but unique. Git’s default uses seven characters, but may be longer or shorter if necessary, provided that the SHA-1 unequivocal and generally eight to ten characters are more than enough to be unique within a project. For example:

$ git log --abbrev-commit --pretty=oneline
ca82a6d changed the version number
085bb3b removed unnecessary test code
a11bef0 first commit

Note: The command --abbrev-commit makes it, instead of showing the hash complete 40 byte hexadecimal confirmation, show only a partial prefix. The default number of digits can be specified with --abbrev = <n> (which also modifies the output of diff, if displayed). Add --pretty = oneline for the output of information to become much more readable for people using terminals of 80 columns.

But how do I easily get the short or full SHA for any commit in your current branch history, useful for reversing and sharing specific code states with others.

You can use the command: git rev-list --max-count=1 --abbrev-commit --skip=# and cramp your fingers, or install the Githash

Explanation of command

  • rev-list: get SHA for any commit in the history of its current branch
  • --max-count=n: Limits the number of commits on the way out.
  • --skip=n: Failure number confirms before starting to show confirmation output.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.