I recently learned about the 4 types of git branches.
Conceptually, I kinda understand, but I’m writing a PowerShell script to get concrete understanding, and I have some questions. Script is partially based on this SO answer. Have put the questions inline with the lines of the script.
$LOCAL_BRANCH = git branch --show-current
# Q1. Is this the local-tracking branch for $LOCAL_BRANCH?
# Or the remote-tracking branch that $LOCAL_BRANCH tracks???
$TRACKING_BRANCH = git config branch.$LOCAL_BRANCH.merge
# Q2. Can you confirm: if above is $null, then $LOCAL_BRANCH is a local-non-tracking branch, right?
if ($null -eq $TRACKING_BRANCH)
{
$Write-Host "on LOCAL-NON-TRACKING BRANCH '$LOCAL_BRANCH"
Exit
}
$REMOTE_NAME = git config branch.$LOCAL_BRANCH.remote
$REMOTE_URL = git config remote.$REMOTE_NAME.url
# Q3. How do I get the name of the remote-tracking branch, if there is one?
# Is it `refs/heads/$LOCAL_BRANCH` or is this just the long name of the local branch?
# Can local branch and remote-tracking branch names have different suffixes? e.g. `dev/foo` and `origin/dev/bar`?
$REMOTE_TRACKING_BRANCH = "?"
# Q4. How do I get the name of the remote branch, if there is one?
# Is it `refs/heads/$REMOTE_NAME/$LOCAL_BRANCH`?
# Do the local, remote-tracking, and remote branches always share the same suffix?
# Or can they diverge?
$REMOTE_BRANCH = "?"
# Q5. Can we have a situation where the remote-tracking branch is tracking a remote branch that does not exists?
# (e.g. if remote branch was deleted) -- have heard of stale branches needing pruning.
# Check for existence of remote branch
$existsInRemote = $null -NE (git ls-remote --heads origin $REMOTE_BRANCH)
$Write-Host " on LOCAL-TRACKING BRANCH '$LOCAL_BRANCH"
$Write-Host " tracking REMOTE-TRACKING BRANCH '$REMOTE_TRACKING_BRANCH'"
$Write-Host " which is tracking REMOTE BRANCH '$REMOTE_BRANCH'"