由于我的电脑打开git bash
很慢很慢,在vscode中我想使用默认的powershell
,但又想看到分支名称,于是倒腾了一下
windows powershell
(或windows terminal
)启动前会先执行一个.ps1
的配置文件,通过修改此文件可以对powershell
做一些预设置。
- 打开
powershell
窗口,输入命令$Profile
查看ps1
文件位置。
$Profile
C:\Users\hnxyc\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
变量$Profile
保存了ps1
文件的绝对路径。
ps1
文件默认不存在,按路径手动创建文件即可。也可以敲下面的命令快速创建。
New-Item –Path $Profile –Type File –Force
- 编辑
ps1
文件
notepad $Profile
在弹出的记事本里将下面的代码拷进去,即可。
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
}
$OutputEncoding = [console]::InputEncoding = [console]::OutputEncoding = New-Object System.Text.UTF8Encoding
$OutputEncoding
是为了支持分支名为中文的情况。