Why is the reduced version of git SHA1 code 7 characters long by default?

7

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

Examples:

git cherry-pick ae33630

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

In this way, I would like to know what the Git reason to accept as a parameter is a reduced hash and why it is specifically 7 characters.

In addition, one more question arises: being a reduced version, could not different hash collisions start with the same characters?

    
asked by anonymous 07.08.2017 / 15:04

1 answer

8

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

In addition to all this, Git contains tools for selecting review , 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 that you may need at some point. And one of them is Short SHA-1 .

Refresh the question: Could not collisions of different hashs start with the same characters?

Git is smart enough to figure out what you intend to type if you provide the first few hash , as long as your SHA-1 has at least four characters and is not ambiguous , that is, only one hash in the current repository that starts with partial SHA-1 .

Git can find a short, single abbreviation for its SHA-1 values using the --abbrev-commit command for the git log command. The output will be shorter, but unique hash . The Git pattern uses seven characters , but may be longer or shorter if needed, as long as the unambiguous SHA-1 is maintained, and usually 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 --abbrev-commit command causes, instead of displaying the 40-byte hexadecimal acknowledgment hash , only show 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 to the information output if it becomes much more readable to people using 80-column terminals.

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

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

Explanation of the command

  • rev-list : get SHA for any commit in the history of your current branch
  • --max-count=n : Limits the number of commits in the output.
  • --skip=n : The failure number confirms before starting to show the confirmation output.
07.08.2017 / 15:47