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
    Serving Fonts from CDN

    For maximum performance, we all know we must put our assets on CDN (another domain).  Along with those assets are custom web fonts.  Unfortunately custom web fonts via CDN (or any cross-domain font request) don't work in Firefox or Internet Explorer (correctly so, by spec) though...

  • By
    Welcome to My New Office

    My first professional web development was at a small print shop where I sat in a windowless cubical all day. I suffered that boxed in environment for almost five years before I was able to find a remote job where I worked from home. The first...

Incredible Demos

  • By
    Image Reflection with jQuery and MooTools

    One subtle detail that can make a big difference on any web design is the use of image reflections. Using them too often can become obnoxious but using reflections on large, "masthead" images is a classy enhancement. Unfortunately creating image reflections within your...

  • By
    Create Custom Events in MooTools 1.2

    Javascript has a number of native events like "mouseover," "mouseout", "click", and so on. What if you want to create your own events though? Creating events using MooTools is as easy as it gets. The MooTools JavaScript What's great about creating custom events in MooTools is...

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!