39

Is is possible to display current git branch name in regular windows command prompt?
Let's say on windows 7 or 10.

CoR
  • 3,826
  • 5
  • 35
  • 42
  • 1
    `git status` shows your current branch on the first line. `git branch -a` lists all your local branches with the current one marked with a `*`. Is this what you want? – Daniel Luz Mar 16 '16 at 22:59
  • 2
    Of course not :) You are talking about pure git commands and I am talking about win command prompt similar to bash from Linux. – CoR Mar 16 '16 at 23:36
  • Simpler answer, applicable everywhere: https://stackoverflow.com/a/1418022/736151 – Joël Feb 25 '19 at 16:18

7 Answers7

20

You cannot do this from the Windows-based "Command Prompt." There is an environment, however, that can run on top of your Windows environment, that does.

After you run the Git windows installer and allow git to install, you can invoke git-bash from context menu.

Call Git Bash in the system menu

By default, the git-bash environment shows what branch you are in.

Screenshot of the git-bash running environment

macetw
  • 1,640
  • 1
  • 17
  • 26
Amr Lotfy
  • 2,937
  • 5
  • 36
  • 56
  • I somehow was using Git CMD and I was not able to see my branch name. Installed git, which installed git bash, I was then able to see my branch name. – Michael Kassa Oct 01 '19 at 09:00
  • also, git bash can be integrated as your default terminal inside VS Code – h3nr1ke Apr 28 '20 at 15:28
  • Just a comment here, I was using git bash but the reason I need to use CMD is because of the currently broken functionality surround node and Ctrl+C in git bash. – Zach Pedigo Jun 16 '20 at 14:17
20

Not in command prompt but in PowerShell and Windows Terminal, it is possible. You can even have custom themes for terminal. Here are the steps:

  1. You obviously need Git to begin with. Install Git for windows from Git website
  2. Then download "Windows Terminal" app from "Windows Store". It has multiple capabilities and new features and you can open multiple tabs of different terminals on the same window and so on.
  3. After installing "Windows Terminal", open it and and now you should install "Posh-Git" and "Oh-My-Posh" using PowerShell: (You may need to install NuGet if you don't already have it. Your PowerShell command line will ask if you want to install NuGet if this is the case. Select [Y] Yes. You may also need to approve that you are installing modules from PSGallery, an 'untrusted repository'. Select [Y] Yes.)
Install-Module posh-git -Scope CurrentUser
Install-Module oh-my-posh -Scope CurrentUser
  1. Now open your "PowerShell" profile with notepad $PROFILE and add these lines to end of the file: (this is a different profile than the "Windows Terminal" profile. PowerShell profile is a script that runs every time PowerShell starts.). If Set-PoshPrompt does not work here try to use Set-Theme command instead (https://superuser.com/questions/1629589/powershell-theming-doesnt-work-set-theme-not-found).
Import-Module posh-git
Import-Module oh-my-posh
Set-PoshPrompt Paradox
  1. Now as you see some characters on the terminal are squares because the default font doesn't support them. (You may not have see this problem based on your terminal's font. if you see all of the characters fine and not squares, skip this step) You want to install "Cascadia PL" fonts to support those characters. Download them from Cascadia Code release page. You need to install these versions of the font in order for it to support the characters:
Cascadia Code PL
Cascadia Mono PL
  1. Then, in Windows Terminal, press Ctrl+, to open the Windows Terminal profile settings in "settings.json" and add these lines to the "defaults" section of the "profiles" as shown below:
"fontFace": "Cascadia Code PL"

How to go to the Windows Terminal settings picture guide

Where to change the font face picture guide

PS 1: If you want to have these changes on the integrated terminals such as the one on the VS Code, you should add this line to the settings of the VS Code:

"terminal.integrated.fontFamily": "Cascadia Code PL"

PS 2: In order to know more about "Oh-My-Posh" and change the theme of it, visit Oh-My-Posh's github page for more information.


Sources:

https://learn.microsoft.com/en-us/windows/terminal/tutorials/powerline-setup https://www.hanselman.com/blog/how-to-make-a-pretty-prompt-in-windows-terminal-with-powerline-nerd-fonts-cascadia-code-wsl-and-ohmyposh

Brian Hasden
  • 4,050
  • 2
  • 31
  • 37
Farid Shokri
  • 304
  • 3
  • 8
  • please accept the answer if u find it useful @CoR – Farid Shokri Oct 15 '21 at 12:51
  • 1
    This worked for me (Windows 11), with some notes: 1) Had to use Set-PoshPrompt. 2) I had to restart my terminal a couple of times, because the first time it didn't download properly. 3) I had to guess which font folder in the ZIP to install, I picked TTF. 4) I had to restart VSCode a couple of times because it didn't take the font setting the first time – tom Feb 28 '22 at 13:35
  • Work but I had to remove Set-Theme line from profile because it throws an error on windows 10: `The term 'Set-Theme' is not recognized as the name of a cmdlet, function, script file, or operable program` – Code Spirit Apr 01 '22 at 15:01
  • You seem to be conflating terminal and shell. Windows Terminal is a terminal, the "command prompt" is a shell. A shell runs in a terminal. Cmd.exe, the regular command prompt, indeed doesn't support this, but this is regardless in which terminal it runs (the new Windows Terminal or the old conhost) – oisyn Jan 04 '23 at 08:10
  • I specifically said that it's not possible in command prompt but since windows 10 has mentioned in the question and has powershell (which is a shell), I think this answer will suffice. @oisyn – Farid Shokri Jan 29 '23 at 21:30
  • @FaridShokri You also said that it was possible in Windows Terminal, which is a weird thing to say because that's just a terminal emulator, it doesn't do anything by itself. You'll need to run PowerShell within Windows Terminal, but it's PowerShell that's making it possible. Do you know it doesn't work in PS opened conhost? – oisyn Jan 30 '23 at 22:51
16

This is the git.bat I am using. I got the answer from the following link:

https://www.nu42.com/2016/05/display-git-branch-windows-command-prompt.html

First, create the git.bat file in a folder, then add the folder to the PATH and ahead of the line to git.exe (I assume you already have the git.exe installed in your computer). This will make sure every time you type git in your command line, the new git.bat will be triggered instead of the git.exe.

@echo off
git.exe %*
set GITBRANCH=
for /f %%I in ('git.exe rev-parse --abbrev-ref HEAD 2^> NUL') do set GITBRANCH=%%I

if "%GITBRANCH%" == "" (
  prompt $P$G 
) else (
    prompt $P $C$E[32;7;32;47m%GITBRANCH%$E[0m$F $G 
)
Richard Liu
  • 161
  • 1
  • 4
  • Nice but it requires a git command to be executed before the branch name is updated on the prompt. (also, I couldn't make git.bat to override git.exe) – FractalSpace Aug 28 '19 at 14:03
  • 1
    The system may be searching for the .exe before the .bat. You could rename git.exe to something else and then use that name to call the correct .exe file in the .bat file. – Zach Pedigo Jun 16 '20 at 14:19
3

If you are using Windows PowerShell, you can override the standard "Prompt" function by running following one in the PowerShell window you are using. This will make it detect Git repositories and list the Git branch in the prompt string:

Function Prompt
{
    $git_cmd = "git rev-parse --abbrev-ref HEAD"
    Invoke-Expression $git_cmd 2> $null | Tee-Object -Variable git_branch | Out-Null
    $git_branch_text = $None
    if ( $git_branch -And -Not $git_branch.StartsWith($git_cmd)) 
    {
        $git_branch_text = "[$git_branch] "
    }

    $stringBuilder = New-Object System.Text.StringBuilder
    $null = $stringBuilder.Append("PS ")
    if ($git_branch_text) { $null = $stringBuilder.Append($git_branch_text) }
    $null = $stringBuilder.Append($($executionContext.SessionState.Path.CurrentLocation))
    $null = $stringBuilder.Append($('>' * ($nestedPromptLevel + 1)))
    $null = $stringBuilder.Append(" ")
    return $stringBuilder.ToString()
}

PS C:\Users\username\Document\GitHub>
PS C:\Users\username\Document\GitHub>cd code_repository
PS [dev] C:\Users\username\Document\GitHub\code_repository>

PS [dev] C:\Users\username\Document\GitHub\code_repository>cd ..
PS C:\Users\username\Document\GitHub>

Tested on PowerShell version 5.1.17763.592.

To apply this change of prompt in all of your PowerShell command windows, put this function in a file named profile.ps1 in C:\Users\username\Documents\WindowsPowerShell. Any PowerShell command windows opened after that should have the Git prompt when in a directory that is part of a Git repository.

DocOc
  • 635
  • 7
  • 13
  • 1
    See also https://stackoverflow.com/questions/1287718/how-can-i-display-my-current-git-branch-name-in-my-powershell-prompt – DocOc Sep 03 '19 at 17:35
2

Yes, you can. You can create a script and name it 'gb.bash' then add the following code to it

@echo off
set GITBRANCH=
for /f "tokens=2" %%I in ('git.exe branch 2^> NUL ^| findstr /b "* "') do set GITBRANCH=%%I

if "%GITBRANCH%" == "" (
    prompt $P$G 
) else (
    prompt $P $C$E[10;7;32;47m%GITBRANCH%$E[0m$F $G 
)

Then go to your environment variables and add the folder that contains this script to your path.

With that in place, simply firing the command 'gb' on your terminal while in a git repository will place the branch name on the path of the folder you're currently on.

Currently, this solution requires that you manually fire the command 'gb' to trigger the script. However, I'm sure there's also a way to trigger the script automatically when you launch CMD. I'll look into that some time and update this answer.

I got the solution from these two sources:

Show git branch in Windows command prompt

Display git branch in Windows command prompt

Disclaimer: You'll have to fire 'gb' after switching to a new branch to update the displayed branch name.

Wachaga Mwaura
  • 3,310
  • 3
  • 28
  • 31
1

I am using babun (discontinued)
It provides an awesome command-line view for git. It also supports context menu and can be open from any location on right-click.

snapshot

dkb
  • 4,389
  • 4
  • 36
  • 54
  • 1
    just for future reference, babun is [discontinued](https://github.com/babun/babun/issues/867) – h3nr1ke Apr 28 '20 at 15:26
  • 1
    yes, sadly :(, one more best software discontinued. But still using it without any issues. – dkb Apr 29 '20 at 05:53
  • Babun is a "pre-configured Cygwin". Thus, "git bash" can also be used. See other answers. -- I am on WSL2 most of the time. My hint for that is to use [Oh My BASH!](https://ohmybash.nntoan.com/) for tweaking the bash. – koppor Mar 17 '23 at 06:39
0

@Farid Shokri is not quite right.

Not in command prompt but in PowerShell [...]

There's no native way but you can customize the cmd behaviour with AutoRun, Doskey, Prompt and a custom batch script.

Solution

  1. Create a batch script cd-git.bat (see below). We place it in C:\Apps.

  2. Create a AutoRun string value (either or)

    • HKLM\Software\Microsoft\Command Processor\AutoRun
    • HKCU\Software\Microsoft\Command Processor\AutoRun

    String Value:

    if exist C:\Apps\cd-git.bat doskey cd=C:\Apps\cd-git.bat $*
    

cd-git.bat

@echo off
CD %*
where git >nul 2>&1
if %errorlevel% neq 0 (
    goto :eof
)
for /f "usebackq tokens=* delims=" %%g in (`git rev-parse --is-inside-work-tree ^>nul 2^>^&1 ^&^& git branch --show-current`) do (
    set branchname=%%g
)
if "%branchname%"=="" (
    prompt $p$_$$$s
) else (
    prompt $p $c$e[36m%branchname%$e[0m$f$_$$$s
)
set branchname=

Result

Example: Start a new cmd and navigate to a git repository.

Microsoft Windows [Version 10.0.19041.2673]
(c) Microsoft Corporation. All rights reserved.

C:\Users>cd \Develop\git-test

C:\Develop\git-test (main)
$ cd ..

C:\Develop
$

As soon as you cd into a git repository directory its current branch will be displayd within the parenthesis.

Explanation

  • The AutoRun entry creates a new alias (macro) for the cd command by using the doskey command but only if our custom batch script cd-git.bat exists.
  • The doskey cd alias ensures that by using cd cd-git gets executed.
  • cd-git
    1. Most important: cds into the requested directory.
    2. Checks whether or not the git command is available.
    3. If so it checks whether there's a branch name for the current directory.
    4. If so changes the prompt to display the branch name as well.

References

urbanSoft
  • 686
  • 6
  • 14