Displaying GIT Branch on your PowerShell prompt
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:
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:
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.
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:
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
6 Comments to Displaying GIT Branch on your PowerShell prompt
Leave a comment
Blogroll
Recent Posts
- Autocompleting ssh, rake, cap command parameters using PowerShell
- Using RubyAmf for creating a CRUD application in Rails
- Multiple ways to open PowerShell in the current Explorer window
- Context sensitive auto-completion using PowerShell, PowerTab and GIT
- Displaying GIT Branch on your PowerShell prompt
XBox Live
Songs I like
Categories
- adobe (4)
- amarok (1)
- C# (2)
- chat application (1)
- crud (2)
- cygwin (1)
- explorer (1)
- expression blend (1)
- flex (4)
- FOSS (2)
- free (1)
- GIT (3)
- ID3 (1)
- java (1)
- microsoft (3)
- mxml (2)
- nokia (1)
- open source (5)
- perl (1)
- powerpoint (2)
- powershell (3)
- rails (3)
- right click (1)
- ruby (9)
- ruby on rails (3)
- security (3)
- silverlight (4)
- smart playlist (1)
- socket (1)
- software (7)
- ssh (1)
- sudo (1)
- tabs (1)
- tomcat (1)
- user interface (6)
- vista (9)
- vista, security (2)
- visual studio 2008 (1)
- win32ole (1)
- windows (13)
- windows media player (1)
- wpf (1)
- xaml (4)
- XP (3)

[...] 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 [...]
[...] your PowerShell profile file (for help see here) and create a function with name "global:TabExpansion" in it. This function basically [...]