我想要获取git日志中前40个字符,但是我当前的命令无法实现:
git log <branch_name> | cut -c 1-40 >> some_file
这会输出整个日志。
我对Linux还比较陌生,有什么建议吗?
编辑:
git log <branch_name> | head -n1 >> some_file
根据@Someprogrammerdude的建议,以下是有效的命令:
| head
方法是很好的,head
程序是一个通用的过滤器,用于提取输入流的前部分或一些输入文件的前部分,但值得注意的是,默认的git log
输出的第一行由单词commit
和提交哈希组成,这个哈希恰好拼写出了40个字符:
$ git log | head -n 1
commit 8f60064c1f538f06e1c579cbd9840b86b10bcd3d
由于commit
(包括尾随空格)长度为8个字符,如果将其截断为40个字符,则可以获得32个字符的提交ID缩写。
由于git log
通常从显示HEAD
提交开始,这意味着你正在获取(部分)HEAD
提交的哈希ID,并且在Git中有一种更直接的方法来做到这一点:
$ git rev-parse HEAD
8f60064c1f538f06e1c579cbd9840b86b10bcd3d
这个方法省略了单词commit
(以及空格),但是得到了我猜想您关心的40个字符。通过添加--short
或--short=count
,您可以将哈希值缩短为任意长度:
This method omits the word commit
(and the space), but gets you the 40 characters that I suspect you care about. You can shorten the hash to any number of characters you like by adding --short
or --short=count
:
$ git rev-parse --short=12 HEAD
8f60064c1f53
通常,将单个名称(如master
、标签名称或HEAD
)转换为Git对象标识符(SHA-1哈希)的方法是使用git rev-parse
命令。
git merge-base --is-ancestor
。因此,如果问题是:“提交1234567是否合并到了主分支”,则直接的shell脚本测试是:if git merge-base --is-ancestor 1234567 master; then ...; else ...; fi
。如果问题是“哪些分支名称是master
的祖先”,则使用git branch --merged master
。(在足够现代的Git中,--merged
谓词也可以在git for-each-ref
中使用。) - torek基本上,它会回显多行文本。这样你的命令就可以从每行中剪切40个字符。
如果你真的想要前40个字符而不考虑行模式,这是命令:
git log <branch_name> | awk '{print substr($0,1,40);exit}'
head
的工作。 - Some programmer dude