Skip to content

Instantly share code, notes, and snippets.

@aravindkumarsvg
Last active April 30, 2024 23:14
Show Gist options
  • Save aravindkumarsvg/f8b9be6724725a02574e252c3fad0c9c to your computer and use it in GitHub Desktop.
Save aravindkumarsvg/f8b9be6724725a02574e252c3fad0c9c to your computer and use it in GitHub Desktop.
Sets the git branch name in the powershell prompt like git bash
# Add this function to the Profile script
# Use the following command to generate the Profile script
#
# $profileTest = Test-Path $PROFILE
# if ($profileTest -eq $FALSE) {
# New-Item $PROFILE -Type File
# }
#
# OR
#
# For all the users and all shell, add this to the following script,
# C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1
# Sets the Prompt which contains the Current git branch name
# Prompt format - current_directory [ current_branch ] >
function prompt {
# redirects error to null
# Gets the current branch which will contains '*' at the front
$currentBranchExt = $((git branch) -match "\*");
if ($currentBranchExt) {
Try {
# Holds the pattern for extracting the branch name
$currentBranchMatchPattern = "\w*";
# Executes the regular expression against the matched branch
$currentBranchNameMatches = [regex]::matches($currentBranchExt, $currentBranchMatchPattern);
# Gets the current branch from the matches
$currentBranchName = $currentBranchNameMatches.Captures[2].Value.Trim();
# Sets the Prompt which contains the Current git branch name
# Prompt format - current_directory [ current_branch ] >
"PS $($executionContext.SessionState.Path.CurrentLocation)$(' [ ' + $currentBranchName + ' ] >' * ($nestedPromptLevel + 1)) ";
}
Catch {
# Calls the default prompt
"PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) ";
}
} else {
# Calls the default prompt
"PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) ";
}
}
@mz-suzin
Copy link

Thanks :)

@codingrivista
Copy link

Thank you

@ktyagiLinz
Copy link

Working for master branch only

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment