Last active
November 10, 2023 09:07
-
-
Save jeroenvdgulik/701942 to your computer and use it in GitHub Desktop.
Output the current branch in your PHP application
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
# Git branch output in PHP | |
exec('git symbolic-ref HEAD', $output); | |
$branch = end(explode('/', $output[0]))); |
entitycs
commented
Aug 23, 2018
•
This produces a Notice
$t = explode(...)
$branch = end($t);
to fix
just use
$branch = trim(shell_exec('git rev-parse --abbrev-ref HEAD'));
just use
$branch = trim(shell_exec('git branch --show-current'));
--show-current was not available in 2018, but going forward, it looks much nicer.
To get sub-module branch name or when multiple git repos are in use:
$path = 'path/to/folder/with/git/repo';
$branch = trim(shell_exec('cd "${PWD}' . $path . '"; git rev-parse --abbrev-ref HEAD'));
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment