Skip to content

Instantly share code, notes, and snippets.

@parsley42
Last active November 10, 2018 23:04
Show Gist options
  • Save parsley42/11745b591ccdde775342ee5b385e0aaa to your computer and use it in GitHub Desktop.
Save parsley42/11745b591ccdde775342ee5b385e0aaa to your computer and use it in GitHub Desktop.
Git Scripting Cheatsheet

Handy git commands for scripting

Get the commit hash

$ git rev-parse HEAD
# Short version
$ git rev-parse --short HEAD
# Previous commit
$ git rev-parse HEAD~1

Get the current branch

$ git rev-parse --abbrev-ref HEAD

Get parents of a merge commit (for e.g. protected branch)

# Current branch parent
$ git show -s --format="%H" master^1
# Merged branch parent
$ git show -s --format="%H" master^2

Get the root directory of the repository

$ git rev-parse --show-toplevel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment