Show current git branch name on windows command line

Is it possible to display the current git branch name on a windows command prompt?

Say on windows 7 or 10.

+14
source share
4 answers

You cannot do this from the Windows command prompt. However, there is an environment that can run on top of your Windows environment.

After starting the Windows Git installer and allowing git installation, you can invoke git-bash from the context menu.

Call Git Bash in the system menu

By default, git-bash shows which branch you are in.

Screenshot of the git-bash running environment

+8
source

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

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

First create the git.bat file in the folder, then add the folder to PATH and before the git.exe line (I assume that git.exe is already installed on your computer). This will ensure that every time you type git at the command line, a new git.bat file will be launched instead of 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 ) 
+5
source

I use Baboon. It provides an amazing command line view for git. It also supports a context menu and can be opened from anywhere by right-clicking.

snapshot

0
source

If you use Windows PowerShell, you can override the standard Hint function by running the following in the PowerShell window that you are using. This will force it to detect Git repositories and list the Git branch at the prompt:

 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 in PowerShell version 5.1.17763.592.

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

0
source

Source: https://habr.com/ru/post/1245197/


All Articles