Search code examples
gitpowershellcommand-prompt

How can I display my current git branch name in my PowerShell prompt?


Basically I'm after this but for PowerShell instead of bash.

I use git on windows through PowerShell. If possible, I'd like my current branch name to displayed as part of the command prompt.


Solution

  • An easier way would be just installing the Powershell module posh-git. It comes out of the box with the desired prompt:

    The Prompt

    PowerShell generates its prompt by executing a prompt function, if one exists. posh-git defines such a function in profile.example.ps1 that outputs the current working directory followed by an abbreviated git status:

    C:\Users\Keith [master]>

    By default, the status summary has the following format:

    [{HEAD-name} +A ~B -C !D | +E ~F -G !H]

    (For installing posh-git I suggest using psget)

    If you don't have psget use the following command:

    (new-object Net.WebClient).DownloadString("https://raw.githubusercontent.com/psget/psget/master/GetPsGet.ps1") | iex
    

    To install posh-git use the command: Install-Module posh-git

    To ensure posh-git loads for every shell, use the Add-PoshGitToProfile command.