Here's my situation:
I develop locally on my Mac using MAMP (PHP). My sites are under Git version control, and I point my dev servers to the root of the site under version control on disk.
File structure: --mysitehere/ ---.git/ (.git folder is here versioning everything below) ---src/ (<-- web server root) ----index.php (need the codez here for displaying current git branch)
Anyone have example code that I could use that looks in the .git folder and sees what the current branch is, and output it on the index.php page (and a ruby solution for RoR dev)? This would be super useful when I switch branches, and in my browser when I refresh, I see that I would be on 'master' at the top of the page, or 'your-topic-branch-name-here'.
I'm willing to use a third-party library that accesses git programmatically in PHP, or something that gets the right 'current-branch' variable from a file on disk from within .git.
The git_branch() is a function, that prints the name of the current Git branch in the round brackets. We set the PS1 variable and place the function git_branch() inside it to display the Git branch in the terminal prompt.
The git show head is used to check the status of the Head. This command will show the location of the Head. Syntax: $ git show HEAD.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With