Is is possible to display current git branch name in regular windows command prompt?
Let's say on windows 7 or 10.
Is is possible to display current git branch name in regular windows command prompt?
Let's say on windows 7 or 10.
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:
Install-Module posh-git -Scope CurrentUser
Install-Module oh-my-posh -Scope CurrentUser
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
Cascadia Code PL
Cascadia Mono PL
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://docs.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
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
)
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.
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.
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.