查找当前在 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
Find which commit is currently checked out in Git
提问by Steven Lu
I'm in the middle of a git bisect
session.
我正在git bisect
开会。
What's the command to find out which commit (SHA1 hash) I am currently on? git status
does not provide this.
找出我当前所在的提交(SHA1 哈希)的命令是什么?git status
不提供这个。
Edit: I guess calling git log
and 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 bisect
session (note that options 1-4 will also work when you're not doing a bisect):
您至少有 5 种不同的方式来查看您当前在git bisect
会话期间检出到工作副本中的提交(请注意,选项 1-4 在您不进行 bisect 时也可以使用):
git show
.git log -1
.- Bash prompt.
git status
.git bisect visualize
.
git show
.git log -1
.- Bash 提示。
git status
.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 show
with the -s
option 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 -1
to 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 c1abcde
checked 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 status
will 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 visualize
or its built-in alias git bisect view
to 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
回答by Mark Longair
You can just do:
你可以这样做:
git rev-parse HEAD
To explain a bit further: git rev-parse
is git's basic command for interpreting any of the exotic ways that you can specify the name of a commitand HEAD
is a reference to your current commit or branch. (In a git bisect
session, 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 HEAD
points 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 show
with 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
的完整列表在. 但这里有一个可能有用的属性的缩写列表:
%H
commit hash%h
abbreviated commit hash%T
tree hash%t
abbreviated tree hash%P
parent hashes%p
abbreviated parent hashes%an
author name%ae
author email%at
author date, UNIX timestamp%aI
author date, strict ISO 8601 format%cn
committer name%ce
committer email%ct
committer date, UNIX timestamp%cI
committer date, strict ISO 8601 format%s
subject%f
sanitized subject line, suitable for a filename%gD
reflog selector, e.g., refs/stash@{1}%gd
shortened 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
清理主题行,适用于文件名%gD
reflog 选择器,例如 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
,它还向您显示提交消息,并且在没有给定参数时默认为当前提交。