Find which commit is currently checked out in Git
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 commit and 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.)
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:
$ git show --oneline -s
c0235b7 Autorotate uploaded images based on EXIF orientation
$ 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 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
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.
For instance, to get just the shortened hash (%h
) you could say:
$ 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:
$ 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:
%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}
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):
git show
.git log -1
.- Bash prompt.
git status
.git bisect visualize
.
I'll explain each option in detail below.
Option 1: git show
As explained in this answer to 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 show --oneline -s
a9874fd Merge branch 'epic-feature'
Option 2: git log -1
You can also simply do git log -1
to find out which commit you're currently on.
$ git log -1 --oneline
c1abcde Add feature-003
Option 3: Bash prompt
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:
# Prompt during a bisect
user ~ (c1abcde...)|BISECTING $
# Prompt at detached HEAD state
user ~ (c1abcde...) $
Option 4: git status
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 status
# HEAD detached at c1abcde <== RIGHT HERE
Option 5: git bisect visualize
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:
git bisect visualize
git bisect view # shorter, means same thing