bash 通过短散列获取 GIT 提交消息的更好方法?

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

Better way of getting a GIT commit message by short hash?

gitbashawkgrepgit-log

提问by ehime

I am currently getting my commit message for a certain commit hash by using this below:

我目前正在通过使用以下内容获取某个提交哈希的提交消息:

hash='b55da97'
git log --pretty=oneline ${hash} | grep "${hash}" | awk '{ print  }'

These seems extremely inefficient though. Is there a smarter or cheaper way to do this, or am I stuck with grepping and awking?

不过,这些似乎效率极低。有没有更聪明或更便宜的方法来做到这一点,还是我坚持使用 grepping 和 awking?

回答by torek

git logtakes (among other things):

git log需要(除其他外):

  • -n numto limit the number of commits shown: choose 1 (and if numis 9 or less you can just write -num, hence, -1, for short)
  • --pretty=format:string with directivesto change the log output format. The %sdirective gets the commit "subject", which is what you also get with oneline.
  • -n num限制显示的提交数量:选择 1(如果num是 9 或更少,您可以只写,因此,简称 )-num-1
  • --pretty=format:string with directives更改日志输出格式。该%s指令获取提交“主题”,这也是您使用oneline.

Hence: git log -n 1 --pretty=format:%s $hash(or git log -1 --pretty=format:%s) will do the trick here.

因此:(git log -n 1 --pretty=format:%s $hashgit log -1 --pretty=format:%s)将在这里起作用。

For a complete list of format directives, see the git log documentation, under "PRETTY FORMATS" (about halfway down).

有关格式指令的完整列表,请参阅“PRETTY FORMATS”下的git log 文档(大约一半)。

回答by twalberg

Depending on how much of the commit message you actually want, there are several pretty-format specifiers that you can use:

根据您实际想要的提交消息的数量,您可以使用几个漂亮的格式说明符:

      ·  %s: subject
      ·  %f: sanitized subject line, suitable for a filename
      ·  %b: body
      ·  %B: raw body (unwrapped subject and body)

So something like git log -1 --pretty=format:%b <hash>, or use one of the other specifiers (I think %sis probably closer to what you are doing now). The -1limits git logto just the one commit, rather than walking the history tree.

所以像git log -1 --pretty=format:%b <hash>, 或使用其他说明符之一(我认为%s可能更接近你现在正在做的事情)。该-1限制git log只是一个承诺,而不是走在历史树。

回答by Brad Parks

I like having the important stuff dumped into a single line... Here's what I use, built off other answers on this page:

我喜欢将重要的内容转储到一行中……这是我使用的内容,基于此页面上的其他答案:

git_log_for_commit.sh

git_log_for_commit.sh

IT=$(git log -1 --pretty=format:"%an, %s, %b, %ai"  $*)
echo "$IT"

output

输出

jdoe, WORK1766032 - Added templating engine, WIP, 2013-08-15 14:25:59 +0000

回答by BishopZ

An even shorter answer than is listed here is

比这里列出的更短的答案是

git log --pretty=oneline {your_hash} | grep {your_hash}

git log --pretty=oneline {your_hash} | grep {your_hash}

回答by Jotne

This may short it some

这可能会缩短一些

git log --pretty=oneline ${hash} | awk '##代码##~var {print }' var="${hash}"