How could I display the current git branch name at the top of the page, of my development website?
This worked for me in PHP, including it in the top of my site:
/** * @filename: currentgitbranch.php * @usage: Include this file after the '<body>' tag in your project * @author Kevin Ridgway */ $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 $branchname = $explodedstring[2]; //get the one that is always the branch name echo "<div style='clear: both; width: 100%; font-size: 14px; font-family: Helvetica; color: #30121d; background: #bcbf77; padding: 20px; text-align: center;'>Current branch: <span style='color:#fff; font-weight: bold; text-transform: uppercase;'>" . $branchname . "</span></div>"; //show it on the page
Branch, last commit date and hash
<?php $gitBasePath = '.git'; // e.g in laravel: base_path().'/.git'; $gitStr = file_get_contents($gitBasePath.'/HEAD'); $gitBranchName = rtrim(preg_replace("/(.*?\/){2}/", '', $gitStr)); $gitPathBranch = $gitBasePath.'/refs/heads/'.$gitBranchName; $gitHash = file_get_contents($gitPathBranch); $gitDate = date(DATE_ATOM, filemtime($gitPathBranch)); echo "version date: ".$gitDate."<br>branch: ".$gitBranchName."<br> commit: ".$gitHash; ?>
Example Output:
version date: 2018-10-31T23:52:49+01:00
branch: dev
commit: 2a52054ef38ba4b76d2c14850fa81ceb25847bab
Modification date of file refs/heads/your_branch
is (acceptable) approximation of last commit date (especially fo testing/staging environment where we assume that we will deploy fresh commits (no old ones)).