bash 仅使用 git log 显示 sha1

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20873649/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 09:07:44  来源:igfitidea点击:

Show sha1 only with git log

gitbash

提问by Mark Amery

I want to write a Bash script that loops over the sha1s of commits output by an invocation of git log. However, git loggives me much more output than I want:

我想编写一个 Bash 脚本,通过调用git log. 但是,git log给了我比我想要的更多的输出:

commit 0375602ba2017ba8750a58e934b41153faee6fcb
Author: Mark Amery <[email protected]>
Date:   Wed Jan 1 21:35:07 2014 +0000

    Yet another commit message

    This one even has newlines.

commit 4390ee9f4428c84bdbeb2fed0a461099a6c81b39
Author: Mark Amery <[email protected]>
Date:   Wed Jan 1 21:30:19 2014 +0000

    Second commit message.

commit bff53bfbc56485c4c1007b0884bb1c0d61a1cf71
Author: Mark Amery <[email protected]>
Date:   Wed Jan 1 21:28:27 2014 +0000

    First commit message.

How can I get git logto just output sha1s so that I can loop over them conveniently?

我怎样才能git log只输出 sha1s 以便我可以方便地循环它们?

采纳答案by Magnus B?ck

An alternative to git log --formatis the git rev-listplumbing command. For scripting purposes it's the recommended choice as the interface can be relied on to be stable (although for simple uses like this I'd be surprised if git logisn't sufficiently stable).

另一种方法git log --formatgit rev-list管道命令。出于脚本目的,它是推荐的选择,因为可以依赖于稳定的界面(尽管对于像这样的简单用途,如果git log不够稳定,我会感到惊讶)。

for sha1 in $(git rev-list HEAD) ; do
    : # Do something with $sha1
done

回答by Mark Amery

You can use the --formatargument with a custom formatthat only includes the sha1:

您可以将--format参数与仅包含 sha1的自定义格式一起使用:

git log --format=format:%H

The above command yields output like the following:

上面的命令产生如下输出:

0375602ba2017ba8750a58e934b41153faee6fcb
4390ee9f4428c84bdbeb2fed0a461099a6c81b39
bff53bfbc56485c4c1007b0884bb1c0d61a1cf71

You can loop over the commit hashes in Bash like this:

您可以在 Bash 中循环提交哈希,如下所示:

for sha1 in $(git log --format=format:%H); do
    : # Do something with $sha1
done

This is slightly more verbose than using git rev-list, but may be your only option if you want to use ordering or filtering arguments for git logthat are not supported by git rev-list, like -S.

这比usinggit rev-list稍微冗长一些,但如果您想使用git log不支持的排序或过滤参数,这可能是您唯一的选择git rev-list,例如-S.

回答by Roman Parfinenko

If you want to see only the latest one

如果您只想查看最新的

git rev-list HEAD | head -1