Refactored Codes
How to show git branch in powershell prompt without using any plugin in windows
Whenever I am working with a git repository in commandline, I want to see the current branch name in the prompt string. That would make working with git projects a lot easier. I won't have to check for the current branch, every now and then. In this article, we are going to show the current branch in the prompt itself. You can watch me code this article on youtube. [embed]https://youtu.be/Le3BDh42_eM[/embed] Let us start with the basics. The prompt string displayed in the powershell comes from a prompt  function. Every time prompt is shown, the prompt function gets called. That is the why we are able to see current working directory in prompt. Whenever the directory changes, the prompt displays the latest directory. We want to do the same with the git-branch. We are going to modify the prompt function, and put it in the $PROFILE script. The $PROFILE is the terminal initialization script file. This file runs to provide initial terminal states like variables and functions. In my computer system, the $PROFILE points to C:\Users\anura\Documents\PowerShell\Microsoft.PowerShell_profile.ps1 So, now we know, what to modify (prompt function) and where to modify ($PROFILE script). Let us talk about, how are we going to implement it. If you go in a .git repository, there is a HEAD file. You can see the folder structure of my Git-repository. You can see HEAD file in red rectangle. The HEAD file contains following string ref: refs/heads/date-conversion The last word data-conversion  is the branch name. This file always contains the current branch, where the head of git is pointing to. If we can extract the last word, it will provide us with the current branch name.
$array = (Get-Content -Path .\.git\HEAD) -split '/';
$branchName = $array[$array.Length - 1];
In the above code, we first split the string on '/'  , which gives us an array of strings. Then we access the last element of the array to get the current branch name. But, this code can not run in all the folders. As, all folders are not going to have the .git folder in it. So, we need to add a check to verify if the current folder has git repository or not Test-Path -Path .git\HEAD putting all this together we get following code.
if( Test-Path -Path .\.git\HEAD ) {
  $array = (Get-Content -Path .\.git\HEAD) -split '/';
  $branchName = $array[$array.Length - 1];
}
We first check if the current folder contains the repository or not. If it does, then we extract the current branch name. But, to show the branch name, we have to return the prompt string with branch name added in it. The default prompt string, that comes with powershell is "$($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) " To show, the current branch we have to return the following string, after adding branch name in the string.
"$($executionContext.SessionState.Path.CurrentLocation) ($branchName)$('>' * ($nestedPromptLevel + 1)) ";
Here, branch name is added after the current directory. We can make it look better by adding ANSI Code to make it look colorful. `e[31m makes foreground color red, and `e[m resets the foreground color
"$($executionContext.SessionState.Path.CurrentLocation) (`e[31m$branchName`e[m)$('>' * ($nestedPromptLevel + 1)) ";
The entire code put together looks like this.
function prompt {
  if( Test-Path -Path .git\HEAD ) {
    $array = (Get-Content -Path .git\HEAD) -split '/';
    $branchName = $array[$array.Length - 1];
    return "$($executionContext.SessionState.Path.CurrentLocation) (`e[31m$branchName`e[m)$('>' * ($nestedPromptLevel + 1)) ";
  }
  return "$($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) ";
}
You can see the screenshot, where curent branch is displayed in red. Which keeps changing, as we keeps changing the current branch with git-switch command.