Displaying GIT Branch on your PowerShell prompt

Posted on October 12, 2008. Filed under: GIT, powershell, vista, windows |

PowerShell is a command line shell like the command prompt (cmd.exe) but with a lot more features. It can be downloaded from here.

One of the features is that you can change the default prompt. Prompt can be changed by overriding the function with name “prompt”.

This function can be declared inside the PowerShell profile file which has the path:

C:\Users\Gaurav\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

If the file does not exist then you will need to create this file. Declaring the following function inside the profile file will change the prompt.

   1: function prompt {
   2:   return "prompt> "
   3: }

to:

image

Now, to display the git branch as a prompt change the function to the following:

   1: function prompt {
   2:   $host.ui.rawui.WindowTitle = $(get-location)
   3:   
   4:   $prompt_string = ""
   5:   
   6:   if(Test-Path .git) {
   7:     $prompt_string = "GIT"
   8:     git branch | foreach {
   9:       if ($_ -match "^\*(.*)"){
  10:         $prompt_string += $matches[1] + "> "
  11:       }
  12:     }
  13:   }
  14:   else{
  15:     $prompt_string = "PS> "
  16:   }
  17:  
  18:   Write-Host ($prompt_string) -nonewline -foregroundcolor yellow
  19:   return " "
  20: }

and the prompt will look like:

image

The code basically is checking if current directory is a git project. This can be done by checking if there is a directory with name “.git” in the current directory. If yes then it runs the “git branch” command and then passes the result through a regular expression to get the current branch.

Here is my full PowerShell prompt function:

   1: $Global:CurrentUser = [System.Security.Principal.WindowsIdentity]::GetCurrent()
   2: $UserType = "User"
   3: $CurrentUser.Groups | foreach { if($_.value -eq "S-1-5-32-544") {$UserType = "Admin"} }
   4:  
   5: function prompt {
   6:   if($UserType -eq "Admin") {
   7:     $host.ui.rawui.WindowTitle = "" + $(get-location) + " : Admin"
   8:     $host.UI.RawUI.ForegroundColor = "Yellow"
   9:   }
  10:   else {
  11:     $host.ui.rawui.WindowTitle = $(get-location)
  12:   }
  13:   
  14:   Write-Host("")
  15:   
  16:   $status_string = ""
  17:   
  18:   if(Test-Path .git) {
  19:     $status_string = "GIT"
  20:     git branch | foreach {
  21:       if ($_ -match "^\*(.*)"){
  22:         $status_string += $matches[1]
  23:       }
  24:     }
  25:     
  26:     $git_create_count = 0
  27:     $git_update_count = 0
  28:     $git_delete_count = 0
  29:     
  30:     git status | foreach {
  31:       if($_ -match "modified:"){ 
  32:         $git_update_count += 1
  33:       }
  34:       elseif($_ -match "deleted:"){ 
  35:         $git_delete_count += 1
  36:       }
  37:       elseif($_ -match "added:"){ 
  38:         $git_create_count += 1
  39:       }
  40:     }
  41:     $status_string += " u:" + $git_update_count + " d:" + $git_delete_count + ">"
  42:   }
  43:   else{
  44:     $status_string = "PS>"
  45:   }
  46:  
  47:   Write-Host ($status_string) -nonewline -foregroundcolor yellow
  48:   return " "
  49: }

This function also displays updated and deleted files in the current git project.

image

 

Scripts execution in PowerShell is disabled?

If you just created the profile file then PowerShell may display the following error message when loading this profile we just created:

image

or

File C:\Users\Gaurav\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 cannot be loaded because the execution of scripts is disabled on this system. Please see “get-help about_signing” for more details.
At line:1 char:2
+ .  <<<< ‘C:\Users\Gaurav\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1’

This is because PowerShell does not allow the execution of scripts by default. To fix this open PowerShell in Admin mode and set the execution policy to unrestricted by running the following command:

   1: Set-ExecutionPolicy Unrestricted

Make a Comment

Leave a reply to Manik Cancel reply

6 Responses to “Displaying GIT Branch on your PowerShell prompt”

RSS Feed for Blogging on Technology Comments RSS Feed

[…] across this post Displaying GIT Branch on your PowerShell prompt and built it for subversion. Add this to your […]

Awesome! We’re going to cover it in an upcoming PowerScripting Podcast (powerscripting.net)

Very useful! Write something like this for my mac too 🙂

@Manik: Something like this already exists for bash. You can check it out here:
http://github.com/guides/put-your-git-branch-name-in-your-shell-prompt

[…] Displaying GIT Branch On Your PowerShell Prompt […]

[…] The ‘prompt’ function overrides how the command prompt is generated and allows a great deal of customization.  As I mentioned in the SO post, the inspiration for my Git prompt comes from this blog post. […]


Where's The Comment Form?

Liked it here?
Why not try sites on the blogroll...