Skip to content

Instantly share code, notes, and snippets.

@ethanwhite
Created June 29, 2014 02:56
Show Gist options
  • Save ethanwhite/ba63849c26301f862e4e to your computer and use it in GitHub Desktop.
Save ethanwhite/ba63849c26301f862e4e to your computer and use it in GitHub Desktop.
Get hash of current git HEAD using Python built-ins
import subprocess
process = subprocess.Popen(['git', 'rev-parse', 'HEAD'], shell=False, stdout=subprocess.PIPE)
git_head_hash = process.communicate()[0].strip()
@ethanwhite
Copy link
Author

Tested on both Ubuntu 14.04 and Windows 7.

@rakhmaevao
Copy link

Windows 10 ok

@mshavliuk
Copy link

MacOS 12.4 OK

@Akshay-Ram-Ramchandra
Copy link

Akshay-Ram-Ramchandra commented Jul 20, 2023

(b'', None) doesnt work anymore

ubuntu 22.04

@ethanwhite
Copy link
Author

Still works for me. Also on Ubuntu 22.04. Python 3.10.10.

Python 3.10.10 (main, Mar 21 2023, 18:45:11) [GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> 
>>> process = subprocess.Popen(['git', 'rev-parse', 'HEAD'], shell=False, stdout=subprocess.PIPE)
>>> git_head_hash = process.communicate()[0].strip()
>>> git_head_hash
b'37982577eca010a03dd5b5e23fe30be8f42da9ed'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment