git

Find which commit is currently checked out in Git


I'm in the middle of a git bisect session.

What's the command to find out which commit (SHA-1 hash value) I am currently on? git status does not provide this.

I guess calling git log and looking at first entry works?


Solution

  • 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):

    1. git show.
    2. git log -1.
    3. Bash prompt.
    4. git status.
    5. 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
    

    enter image description here