基本上我想要的是这个,但是用于PowerShell而不是bash。
我在Windows上通过PowerShell使用git。如果可能的话,我希望我的当前分支名称可以显示为命令提示符的一部分。
基本上我想要的是这个,但是用于PowerShell而不是bash。
我在Windows上通过PowerShell使用git。如果可能的话,我希望我的当前分支名称可以显示为命令提示符的一部分。
我使用了与@tamj0rd2上面提到的方法类似但更简单的方法。
打开:Microsoft.PowerShell_profile.ps1
首先添加:
function Get-GitBranch {
if (Test-Path .git) {
$branch = git rev-parse --abbrev-ref HEAD
" [$branch]"
}
}
添加第二个:
function prompt {
$p = Split-Path -leaf -path (Get-Location)
Write-Host "$p" -NoNewline
Write-Host (Get-GitBranch) -NoNewline -ForegroundColor Yellow
Write-Host "> " -NoNewline
return " "
}
function prompt {
$p = Split-Path -leaf -path (Get-Location)
Write-Host "$p" -NoNewline
Write-Host (Get-GitBranch) -NoNewline -ForegroundColor Yellow
Write-Host "> " -NoNewline
return " "
}
这是我在 Windows 11 上使用终端应用程序的最简单方法。它适用于主题化的 PowerShell,在无样式的情况下工作,并在子目录中显示分支。
echo %profile
C:\Users\YourName\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
function prompt {
$gitBranch = & git rev-parse --abbrev-ref HEAD 2>$null
$promptString = "$PWD"
if ($gitBranch) {
$promptString += " ($gitBranch)"
}
"$promptString> "
}
git branch --current
。 - VonCgit branch --show-current
。不确定最初是否为--current
,但截至2023年,它已更改为前者。 - Jacques Mathieu--show-current
。 - VonC