Skip to content

Instantly share code, notes, and snippets.

@mgrubinger
Last active December 4, 2018 07:16
Show Gist options
  • Save mgrubinger/0b504b3b364b5b48769b9e17afe40099 to your computer and use it in GitHub Desktop.
Save mgrubinger/0b504b3b364b5b48769b9e17afe40099 to your computer and use it in GitHub Desktop.
Get current Git Branch in PHP
/*
Get current Git Branch in PHP
See: https://stackoverflow.com/a/7448133/3091226#
*/
$stringfromfile = file('.git/HEAD', FILE_USE_INCLUDE_PATH);
$firstLine = $stringfromfile[0]; //get the string from the array
$explodedstring = explode("/", $firstLine, 3); //seperate out by the "/" in the string
$explodedstring = array_slice($explodedstring , 2);
$branchname = trim(implode("/", $explodedstring));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment