Get the Git Commit ID via Command Line

By  on  

I know just enough git to be dangerous.  I'm not doing advanced bisecting but I can stash, rebase, and reset with the best of them.  One new trick I learned from my boss, Luke Crouch, saves me loads of time:  getting the commit ID via command line.  For years I would merge a PR, go the project's main page, and copy the commit ID so that I could push code to staging and production.  Always seemed like an extra step rather than just making it happen from the terminal.  Here's the magical command:

git rev-parse HEAD

Of course you need to update your local repo to remote master, but you do that anyway, right?  Hopefully this will become a timesaver the same way it has for me!

Recent Features

  • By
    Create Namespaced Classes with MooTools

    MooTools has always gotten a bit of grief for not inherently using and standardizing namespaced-based JavaScript classes like the Dojo Toolkit does.  Many developers create their classes as globals which is generally frowned up.  I mostly disagree with that stance, but each to their own.  In any event...

  • By
    Conquering Impostor Syndrome

    Two years ago I documented my struggles with Imposter Syndrome and the response was immense.  I received messages of support and commiseration from new web developers, veteran engineers, and even persons of all experience levels in other professions.  I've even caught myself reading the post...

Incredible Demos

  • By
    Creating Spacers with Flexbox

    I was one of the biggest fans of flexbox before it hit but, due to being shuffled around at Mozilla, I never had the chance to use it in any practice project; thus, flexbox still seems like a bit of a mystery to me.  This greatly...

  • By
    Checkbox Filtering Using MooTools ElementFilter

    When I first wrote MooTools ElementFilter, I didn't think much of it. Fast forward eight months later and I've realized I've used the plugin a billion times. Hell, even one of the "big 3" search engines is using it for their maps application.

Discussion

  1. On Mac, you should try this, it will copy the commit ID to your clipboard:

    git rev-parse HEAD | pbcopy
  2. Also you can use short version of git hash:

    git rev-parse --short HEAD
    
  3. Daniel Mejia

    I miss working a Mac, but Windows is working out just great too.

    git rev-parse HEAD | clip
  4. Christian Knappke

    To shorten things further, you can use @ as an abbreviation of HEAD.

  5. Adam Stankiewicz

    Here’s npm package if you need it in JS: https://github.com/sheerun/git-commit-id

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!