基本上我在this之后,但对于PowerShell而不是bash。
我通过PowerShell在Windows上使用git。如果可能的话,我希望我的当前分支名称显示为命令提示符的一部分。
答案 0 :(得分:36)
更简单的方法是安装Powershell模块posh-git。它带有所需的提示:
提示
PowerShell通过执行提示函数(如果存在)生成其提示。 posh-git在profile.example.ps1中定义了这样一个函数,它输出当前工作目录,后跟缩写的git状态:
C:\Users\Keith [master]>
默认情况下,状态摘要具有以下格式:
[{HEAD-name} +A ~B -C !D | +E ~F -G !H]
(对于安装posh-git我建议使用psget)
如果你没有psget,请使用以下命令:
(new-object Net.WebClient).DownloadString("http://psget.net/GetPsGet.ps1") | iex
要安装posh-git,请使用以下命令:
Install-Module posh-git
要确保每个shell的posh-git加载,请使用Add-PoshGitToPrompt
command。
答案 1 :(得分:23)
@保罗 -
Git的我的PowerShell配置文件基于我在此处找到的脚本:
http://techblogging.wordpress.com/2008/10/12/displaying-git-branch-on-your-powershell-prompt/
我对它进行了一些修改,以显示目录路径和一些格式。它还设置了我的Git bin位置的路径,因为我使用PortableGit。
# General variables
$pathToPortableGit = "D:\shared_tools\tools\PortableGit"
$scripts = "D:\shared_tools\scripts"
# Add Git executables to the mix.
[System.Environment]::SetEnvironmentVariable("PATH", $Env:Path + ";" + (Join-Path $pathToPortableGit "\bin") + ";" + $scripts, "Process")
# Setup Home so that Git doesn't freak out.
[System.Environment]::SetEnvironmentVariable("HOME", (Join-Path $Env:HomeDrive $Env:HomePath), "Process")
$Global:CurrentUser = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$UserType = "User"
$CurrentUser.Groups | foreach {
if ($_.value -eq "S-1-5-32-544") {
$UserType = "Admin" }
}
function prompt {
# Fun stuff if using the standard PowerShell prompt; not useful for Console2.
# This, and the variables above, could be commented out.
if($UserType -eq "Admin") {
$host.UI.RawUI.WindowTitle = "" + $(get-location) + " : Admin"
$host.UI.RawUI.ForegroundColor = "white"
}
else {
$host.ui.rawui.WindowTitle = $(get-location)
}
Write-Host("")
$status_string = ""
$symbolicref = git symbolic-ref HEAD
if($symbolicref -ne $NULL) {
$status_string += "GIT [" + $symbolicref.substring($symbolicref.LastIndexOf("/") +1) + "] "
$differences = (git diff-index --name-status HEAD)
$git_update_count = [regex]::matches($differences, "M`t").count
$git_create_count = [regex]::matches($differences, "A`t").count
$git_delete_count = [regex]::matches($differences, "D`t").count
$status_string += "c:" + $git_create_count + " u:" + $git_update_count + " d:" + $git_delete_count + " | "
}
else {
$status_string = "PS "
}
if ($status_string.StartsWith("GIT")) {
Write-Host ($status_string + $(get-location) + ">") -nonewline -foregroundcolor yellow
}
else {
Write-Host ($status_string + $(get-location) + ">") -nonewline -foregroundcolor green
}
return " "
}
到目前为止,这一点非常有效。在回购中,提示愉快地看起来像:
GIT [master] c:0 u:1 d:0 | j:\项目\叉\流利-的nhibernate>
*注意:更新了JakubNarębski的建议。
答案 2 :(得分:15)
这是我对它的看法。我对颜色进行了一些编辑,使其更具可读性。
<强> Microsoft.PowerShell_profile.ps1 强>
function Write-BranchName () {
try {
$branch = git rev-parse --abbrev-ref HEAD
if ($branch -eq "HEAD") {
# we're probably in detached HEAD state, so print the SHA
$branch = git rev-parse --short HEAD
Write-Host " ($branch)" -ForegroundColor "red"
}
else {
# we're on an actual branch, so print it
Write-Host " ($branch)" -ForegroundColor "blue"
}
} catch {
# we'll end up here if we're in a newly initiated git repo
Write-Host " (no branches yet)" -ForegroundColor "yellow"
}
}
function prompt {
$base = "PS "
$path = "$($executionContext.SessionState.Path.CurrentLocation)"
$userPrompt = "$('>' * ($nestedPromptLevel + 1)) "
Write-Host "`n$base" -NoNewline
if (Test-Path .git) {
Write-Host $path -NoNewline -ForegroundColor "green"
Write-BranchName
}
else {
# we're not in a repo so don't bother displaying branch name/sha
Write-Host $path -ForegroundColor "green"
}
return $userPrompt
}
示例1:
示例2:
答案 3 :(得分:1)
我调整了提示码(来自@ david-longnecker的答案),使其更加丰富多彩。
编辑: 2018年10月 - 使用额外的评论,一些清理工作,比以前更少的噪音输出进行了重新设计。
powershell代码:
Function Prompt {
$SYMBOL_GIT_BRANCH='⑂'
$SYMBOL_GIT_MODIFIED='*'
$SYMBOL_GIT_PUSH='↑'
$SYMBOL_GIT_PULL='↓'
if (git rev-parse --git-dir 2> $null) {
$symbolicref = $(git symbolic-ref --short HEAD 2>$NULL)
if ($symbolicref) {#For branches append symbol
$branch = $symbolicref.substring($symbolicref.LastIndexOf("/") +1)
$branchText=$SYMBOL_GIT_BRANCH + ' ' + $branch
} else {#otherwise use tag/SHA
$symbolicref=$(git describe --tags --always 2>$NULL)
$branch=$symbolicref
$branchText=$symbolicref
}
} else {$symbolicref = $NULL}
if ($symbolicref -ne $NULL) {
# Tweak:
# When WSL and Powershell terminals concurrently viewing same repo
# Stops from showing CRLF/LF differences as updates
git status > $NULL
$differences = $(git diff-index --name-status HEAD)
If ($differences -ne $NULL) {
$git_create_count = [regex]::matches($differences, "A`t").count
$git_update_count = [regex]::matches($differences, "M`t").count
$git_delete_count = [regex]::matches($differences, "D`t").count
}
else {
$git_create_count = 0
$git_update_count = 0
$git_delete_count = 0
}
#Identify how many commits ahead and behind we are
#by reading first two lines of `git status`
$marks=$NULL
(git status --porcelain --branch 2>$NULL) | ForEach-Object {
If ($_ -match '^##') {
If ($_ -match 'ahead\ ([0-9]+)') {$git_ahead_count=[int]$Matches[1]}
If ($_ -match 'behind\ ([0-9]+)') {$git_behind_count=[int]$Matches[1]}
}
}
$branchText+="$marks"
}
if (test-path variable:/PSDebugContext) {
Write-Host '[DBG]: ' -nonewline -foregroundcolor Yellow
}
Write-Host "PS " -nonewline -foregroundcolor White
Write-Host $($executionContext.SessionState.Path.CurrentLocation) -nonewline -foregroundcolor White
if ($symbolicref -ne $NULL) {
Write-Host (" [ ") -nonewline -foregroundcolor Magenta
#Output the branch in prettier colors
If ($branch -eq "master") {
Write-Host ($branchText) -nonewline -foregroundcolor White
}
else {Write-Host $branchText -nonewline -foregroundcolor Red}
#Output commits ahead/behind, in pretty colors
If ($git_ahead_count -gt 0) {
Write-Host (" $SYMBOL_GIT_PUSH") -nonewline -foregroundcolor White
Write-Host ($git_ahead_count) -nonewline -foregroundcolor Green
}
If ($git_behind_count -gt 0) {
Write-Host (" $SYMBOL_GIT_PULL") -nonewline -foregroundcolor White
Write-Host ($git_behind_count) -nonewline -foregroundcolor Yellow
}
#Output unstaged changes count, if any, in pretty colors
If ($git_create_count -gt 0) {
Write-Host (" c:") -nonewline -foregroundcolor White
Write-Host ($git_create_count) -nonewline -foregroundcolor Green
}
If ($git_update_count -gt 0) {
Write-Host (" u:") -nonewline -foregroundcolor White
Write-Host ($git_update_count) -nonewline -foregroundcolor Yellow
}
If ($git_delete_count -gt 0) {
Write-Host (" d:") -nonewline -foregroundcolor White
Write-Host ($git_delete_count) -nonewline -foregroundcolor Red
}
Write-Host (" ]") -nonewline -foregroundcolor Magenta
}
$(Write-Host $('>' * ($nestedPromptLevel + 1)) -nonewline -foregroundcolor White)
return " "}#Powershell requires a return, otherwise defaults to factory prompt
以下是来自结果的命令,以查看它的外观:
mkdir c:\git\newrepo | Out-Null
cd c:\git\newrepo
git init
"test" >> ".gitignore"
"test" >> ".gitignore2"
git add -A
git commit -m "test commit" | Out-Null
"test" >> ".gitignore1"
git add -A
"test1" >> ".gitignore2"
git rm .gitignore
git add -A
git commit -m "test commit2" | Out-Null
git checkout -b "newfeature1"
git checkout "master"
cd c:\git\test #Just a sample repo had that was ahead 1 commit
答案 4 :(得分:1)
posh-git 很慢,https://ohmyposh.dev/ 有更好的方法。
ohmyposh
模块:Install-Module oh-my-posh -Scope CurrentUser -AllowPrerelease
从 https://www.nerdfonts.com/ 安装支持字形(图标)的字体。
我喜欢Meslo LGM NF。
在 powershell 默认设置中设置该字体:
Microsoft.PowerShell_profile.ps1
打开/创建文件 C:\Program Files\PowerShell\7
并在下面写下设置主题(与屏幕截图相同):Set-PoshPrompt -Theme aliens
您也可以选择其他主题。通过运行 Get-PoshThemes
现在在包含 git repo 的位置打开 powershell,您将看到状态。
答案 5 :(得分:0)
从@ tamj0rd2的答案中,我们可以将分支名称获取为这样的字符串变量。
$branch = git rev-parse --abbrev-ref HEAD
echo $branch
答案 6 :(得分:0)
在Git 2.22(2019年第二季度)中,任何脚本(无论是否使用Powershell)都可以使用the new --show-current
option。
$branch = git branch --show-current
如果为空,则表示“分离头”。
答案 7 :(得分:0)
我喜欢被接受的答案,因此我将详细说明设置它的步骤。
您可以使用Chocolatey或使用适用于新Core PowerShell的PowerShellGet
命令来安装PoshGit。
对于Chocolatey,您需要先安装它,然后再继续。 在管理员/高架外壳程序中,执行以下命令:
choco install poshgit
对于Core PowerShell,还需要安装。要安装Core PowerShell,请执行以下命令:
dotnet tool install --global PowerShell
注意:您需要安装.NET Core SDK(最好是最新版本)
在安装Core PowerShell之后,执行以下命令以安装PoshGit:
PowerShellGet\Install-Module posh-git -Scope CurrentUser -AllowPrerelease -Force
使用PoshGit要求您使用命令Import-Module posh-git将其导入到当前运行的Shell环境中。这意味着您每次打开新的外壳程序都必须运行此命令。
如果希望PoshGit始终可用,则应执行以下命令:
Add-PoshGitToProfile
请注意,PowerShell和Core PowerShell是不同的,因此它们在不同的配置文件上运行。这意味着如果您希望PoshGit在任何一个shell中都可以工作,则需要在它们各自的shell环境中执行这些命令。
答案 8 :(得分:0)
这是我对 PowerShell Core 的配置。只需复制下面的函数并将其放入您的 $PROFILE
function prompt {
try {
$GitBranch = git rev-parse --abbrev-ref HEAD
# we're probably in detached HEAD state, so print the SHA
if ($GitBranch -eq "HEAD") { $GitBranch = git rev-parse --short HEAD }
} catch {}
if ($GitBranch) { $GitBranch = " `e[33;93m[`e[33;96m$GitBranch`e[33;93m]`e[0m" }
"PS $pwd$GitBranch> "
}