Find which commit is currently being checked in Git

I am in the middle of a git bisect session.

Which command do you recognize, which commit (SHA1 hash) do I find now? git status does not provide this.

Edit: I assume git log being called and looking at the first entry?

+81
git
Jun 23 2018-12-12T00:
source share
5 answers

You have at least 5 different ways to view the commit that you currently checked in your working copy during the git bisect session ( note that options 1-4 will also work if you are not doing a bisector ):

  • git show .
  • git log -1 .
  • Bash.
  • git status .
  • git bisect visualize .

I will explain each option in detail below.

Option 1: show git

As explained in this answer to the general question of how to determine which commit you currently have (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 just do git log -1 to find out what commit you are 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 a Bash prompt configured to display the current branch that you checked in your working copy, then it will also show you the current commit that you checked in bisect session time or when you are in a "disconnected HEAD" state. In the example below, I have currently checked c1abcde :

 # Prompt during a bisect user ~ (c1abcde...)|BISECTING $ # Prompt at detached HEAD state user ~ (c1abcde...) $ 

Option 4: git status

Also, as in git version 1.8.3+ (and, probably, earlier, again, not sure), running git status will also show you what commit you checked during bisection and when you are in the offline state of HEAD

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

Option 5: visualize git bisect

Finally, while you are doing git bisect , you can also just use git bisect visualize or its built-in alias, git bisect view run gitk so that you can graphically see what commit you are on, and also for which you have noted as good and bad so far since I am sure that this existed long before version 1.8.3, Iโ€™m just not sure which version it was introduced in:

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

enter image description here

+54
Aug 10 '13 at 8:46
source share

You can simply do:

 git rev-parse HEAD 

To clarify in more detail: git rev-parse is the git main command for interpreting any of the exotic ways that you can specify a commit name and HEAD is a link to your current commit or branch. (In a git bisect session, it points directly to a commit ("separate head"), not a branch.)

Alternatively (and easier to remember) would be simple:

 git show 

... which by default points to the commit that HEAD points to. For a more concise version, you can:

 $ git show --oneline -s c0235b7 Autorotate uploaded images based on EXIF orientation 
+75
Jun 23 2018-12-12T00:
source share
 $ git rev-parse HEAD
 273cf91b4057366a560b9ddcee8fe58d4c21e6cb

Update:

Alternatively (if you have tags):

(Good for naming the version, not good for returning 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
+12
Jun 23 2018-12-12T00:
source share

If you want to extract only a simple piece of information, you can get it using git show with the option --format=<string> ... and ask her not to give you diff with --no-patch . This means that you can get the output in printf format that you want, which may be the only field.

For example, to get only the shortened hash ( %h ), you can say:

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

If you want to store this in an environment variable in bash (probably for people who want to do this), you can use the $() syntax:

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

A complete list of what you can do is in git show --help . But here is an abbreviated list of properties that may be useful:

  • %h commit hash
  • %h reduced commit hash
  • %T tree hash
  • %T abbreviated tree hash
  • %P parent hashes
  • %P abbreviated parent hashes
  • %an author name
  • %ae author email address
  • %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 topic
  • %f sanitized theme string suitable for file name
  • %gD reflog selector, for example refs / stash @ {1}
  • %gD shorthand reflog selector, for example stash @ {1}
+6
Mar 02 '17 at 7:37
source share

Use git show , which also displays a commit message, and defaults to the current commit with no arguments.

+2
Jun 23 '12 at 10:37
source share



All Articles