git 从常规哈希中获取短哈希
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16413373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
git get short hash from regular hash
提问by Omer Dagan
I have a regular long SHA-1 hash string. I would like to get the shortest unambiguous SHA-1 hash string version of it. That is, the one I would get by using git log -1 --pretty=format:%h
, assuming the long hash refers to the last commit.
我有一个普通的长 SHA-1 哈希字符串。我想获得最短的明确 SHA-1 哈希字符串版本。也就是说git log -1 --pretty=format:%h
,假设长散列指的是最后一次提交,我会通过使用得到的。
回答by cexbrayat
The shortest SHA1 you can get has a length of 4. Rev parse will give you a SHA1 of 7 digits by default with the shortoption :
您可以获得的最短 SHA1 的长度为 4。默认情况下,Rev 解析将为您提供 7 位数的 SHA1,并使用short选项:
git rev-parse --short 921103db8259eb9de72f42db8b939895f5651489
921103d
You have to specify 4 to the short option to have the shortest unambiguous SHA1 :
您必须为 short 选项指定 4 以获得最短的明确 SHA1 :
git rev-parse --short=4 921103db8259eb9de72f42db8b939895f5651489
92110
You can also set it in the core.abbrev
configuration variable.
您也可以在core.abbrev
配置变量中设置它。
回答by Omer Dagan
$ git rev-parse --short 1a6f39665adf05810f70b37ef6470bbcf61fcd38
1a6f396