查找当前在 Git 中签出的提交

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

Find which commit is currently checked out in Git

git

提问by Steven Lu

I'm in the middle of a git bisectsession.

我正在git bisect开会。

What's the command to find out which commit (SHA1 hash) I am currently on? git statusdoes not provide this.

找出我当前所在的提交(SHA1 哈希)的命令是什么?git status不提供这个。

Edit: I guess calling git logand looking at first entry works?

编辑:我猜打电话git log并查看第一个条目是否有效?

回答by

You have at least 5 different ways to view the commit you currently have checked out into your working copy during a git bisectsession (note that options 1-4 will also work when you're not doing a bisect):

您至少有 5 种不同的方式来查看您当前在git bisect会话期间检出到工作副本中的提交(请注意,选项 1-4 在您不进行 bisect 时也可以使用):

  1. git show.
  2. git log -1.
  3. Bash prompt.
  4. git status.
  5. git bisect visualize.
  1. git show.
  2. git log -1.
  3. Bash 提示。
  4. git status.
  5. git bisect visualize.

I'll explain each option in detail below.

我将在下面详细解释每个选项。

Option 1: git show

选项 1:git 显示

As explained in this answerto the general question of how to determine which commit you currently have checked-out (not just during git bisect), you can use git showwith the -soption to suppress patch output:

正如在解释这个答案到提交当前已签出(不只是在如何确定的一般问题git bisect),您可以使用git show-s选项禁止补丁的输出:

$ git show --oneline -s
a9874fd Merge branch 'epic-feature'

Option 2: git log -1

选项 2: git log -1

You can also simply do git log -1to find out which commit you're currently on.

您也可以简单git log -1地找出您当前正在进行的提交。

$ git log -1 --oneline
c1abcde Add feature-003

Option 3: Bash prompt

选项 3:Bash 提示

In Git version 1.8.3+ (or was it an earlier version?), if you have your Bash prompt configured to show the current branch you have checked out into your working copy, then it will also show you the current commit you have checked out during a bisect session or when you're in a "detached HEAD" state. In the example below, I currently have c1abcdechecked out:

在 Git 版本 1.8.3+(或者是更早的版本?)中,如果您将 Bash 提示配置为显示您已检出到工作副本中的当前分支,那么它还将显示您已检查的当前提交在二等分会话期间或当您处于“分离的 HEAD”状态时。在下面的示例中,我目前已c1abcde签出:

# Prompt during a bisect
user ~ (c1abcde...)|BISECTING $

# Prompt at detached HEAD state 
user ~ (c1abcde...) $

Option 4: git status

选项 4:git 状态

Also as of Git version 1.8.3+ (and possibly earlier, again not sure), running git statuswill also show you what commit you have checked out during a bisect and when you're in detached HEAD state:

同样从 Git 版本 1.8.3+(可能更早,再次不确定)开始,运行git status还将显示您在二等分期间以及当您处于分离的 HEAD 状态时检查了哪些提交:

$ git status
# HEAD detached at c1abcde <== RIGHT HERE

Option 5: git bisect visualize

选项 5:git bisect 可视化

Finally, while you're doing a git bisect, you can also simply use git bisect visualizeor its built-in alias git bisect viewto launch gitk, so that you can graphically view which commit you are on, as well as which commits you have marked as bad and good so far. I'm pretty sure this existed well before version 1.8.3, I'm just not sure in which version it was introduced:

最后,当你在做 a 时git bisect,你也可以简单地使用git bisect visualize或者它的内置别名git bisect view来启动gitk,这样你就可以以图形方式查看你正在进行的提交,以及到目前为止你标记为坏和好的提交。我很确定这在 1.8.3 版本之前就已经存在,我只是不确定它是在哪个版本中引入的:

git bisect visualize 
git bisect view # shorter, means same thing

enter image description here

在此处输入图片说明

回答by Mark Longair

You can just do:

你可以这样做:

git rev-parse HEAD

To explain a bit further: git rev-parseis git's basic command for interpreting any of the exotic ways that you can specify the name of a commitand HEADis a reference to your current commit or branch. (In a git bisectsession, it points directly to a commit ("detached HEAD") rather than a branch.)

进一步解释一下:git rev-parse是 git 的基本命令,用于解释您可以指定提交名称的任何奇特方式,并且HEAD是对当前提交或分支的引用。(在git bisect会话中,它直接指向提交(“分离的 HEAD”)而不是分支。)

Alternatively (and easier to remember) would be to just do:

或者(更容易记住)就是这样做:

git show

... which defaults to showing the commit that HEADpoints to. For a more concise version, you can do:

...默认显示HEAD指向的提交。对于更简洁的版本,您可以执行以下操作:

$ git show --oneline -s
c0235b7 Autorotate uploaded images based on EXIF orientation

回答by Alexander Gladysh

$ git rev-parse HEAD
273cf91b4057366a560b9ddcee8fe58d4c21e6cb

Update:

更新:

Alternatively (if you have tags):

或者(如果您有标签):

(Good for naming a version, not very good for passing back to git.)

(适合命名版本,不太适合传递回 git。)

$ git describe
v0.1.49-localhost-ag-1-g273cf91

Or (as Mark suggested, listing here for completeness):

或者(正如马克所建议的,为了完整起见,在此列出):

$ git show --oneline -s
c0235b7 Autorotate uploaded images based on EXIF orientation

回答by HostileFork says dont trust SE

If you want to extract just a simple piece of information, you can get that using git showwith the --format=<string>option...and ask it not to give you the diff with --no-patch. This means you can get a printf-style output of whatever you want, which might often be a single field.

如果要提取只是一个简单的资料片,你可以得到使用git show--format=<string>选项...,并要求它不给你的差异--no-patch。这意味着您可以获得任何您想要的 printf 风格的输出,这通常可能是单个字段。

For instance, to get just the shortened hash (%h) you could say:

例如,要获得缩短的哈希 ( %h),您可以说:

$ git show --format="%h" --no-patch
4b703eb

If you're looking to save that into an environment variable in bash (a likely thing for people to want to do) you can use the $()syntax:

如果您想将其保存到 bash 中的环境变量中(人们可能想要这样做),您可以使用以下$()语法

$ GIT_COMMIT="$(git show --format="%h" --no-patch)"

$ echo $GIT_COMMIT
4b703eb

The full list of what you can do is in git show --help. But here's an abbreviated list of properties that might be useful:

您可以执行的操作git show --help的完整列表在. 但这里有一个可能有用的属性的缩写列表:

  • %Hcommit hash
  • %habbreviated commit hash
  • %Ttree hash
  • %tabbreviated tree hash
  • %Pparent hashes
  • %pabbreviated parent hashes
  • %anauthor name
  • %aeauthor email
  • %atauthor date, UNIX timestamp
  • %aIauthor date, strict ISO 8601 format
  • %cncommitter name
  • %cecommitter email
  • %ctcommitter date, UNIX timestamp
  • %cIcommitter date, strict ISO 8601 format
  • %ssubject
  • %fsanitized subject line, suitable for a filename
  • %gDreflog selector, e.g., refs/stash@{1}
  • %gdshortened reflog selector, e.g., stash@{1}
  • %H提交哈希
  • %h缩写提交哈希
  • %T树哈希
  • %t缩写树哈希
  • %P父哈希
  • %p缩写的父哈希
  • %an作者姓名
  • %ae作者邮箱
  • %at作者日期,UNIX 时间戳
  • %aI作者日期,严格的 ISO 8601 格式
  • %cn提交者姓名
  • %ce提交者电子邮件
  • %ct提交者日期,UNIX 时间戳
  • %cI提交者日期,严格的 ISO 8601 格式
  • %s主题
  • %f清理主题行,适用于文件名
  • %gDreflog 选择器,例如 refs/stash@{1}
  • %gd缩短的 reflog 选择器,例如 stash@{1}

回答by asmeurer

Use git show, which also shows you the commit message, and defaults to the current commit when given no arguments.

使用git show,它还向您显示提交消息,并且在没有给定参数时默认为当前提交。