show current GIT branch name in windows command prompt

CoR picture CoR · Mar 16, 2016 · Viewed 19.1k times · Source

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

Answer

Richard Liu picture Richard Liu · Sep 19, 2018

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 
)