Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

show current GIT branch name in windows command prompt

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

like image 717
CoR Avatar asked Mar 16 '16 21:03

CoR


People also ask

How do you check from which branch the current branch is created?

You can use git branch --contains to list all the branches descended from the tip of develop , then use grep to make sure feature is among them. If it is among them, it will print " feature" to standard output and have a return code of 0.

Can I use git commands in CMD?

All you have to do is load Command Prompt (Load the Start menu, then click "Run", type cmd and hit enter), then you can use Git commands as normal.


3 Answers

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

like image 182
Farid Shokri Avatar answered Oct 14 '22 07:10

Farid Shokri


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

like image 28
Amr Lotfy Avatar answered Oct 14 '22 07:10

Amr Lotfy


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 
)
like image 15
Richard Liu Avatar answered Oct 14 '22 05:10

Richard Liu