基本上我是在this之後,但是用PowerShell而不是bash。如何在我的PowerShell提示符中顯示當前的git分支名稱?
我通過PowerShell在Windows上使用git。如果可能,我希望我的當前分支名稱顯示爲命令提示符的一部分。
基本上我是在this之後,但是用PowerShell而不是bash。如何在我的PowerShell提示符中顯示當前的git分支名稱?
我通過PowerShell在Windows上使用git。如果可能,我希望我的當前分支名稱顯示爲命令提示符的一部分。
更簡單的方法是安裝Powershell模塊posh-git。它出來帶有所需提示框的:
提示
PowerShell中通過執行提示功能產生其提示(如果存在)。
C:\Users\Keith [master]>
默認情況下,狀態摘要具有以下格式::
豪華-git的在profile.example.ps1輸出當前工作目錄隨後縮寫git的狀態定義了這樣一種函數
[{HEAD-name} +A ~B -C !D | +E ~F -G !H]
(用於安裝豪華-混帳話,建議用psget)
我F你沒有psget使用以下命令:
(new-object Net.WebClient).DownloadString("http://psget.net/GetPsGet.ps1") | iex
要安裝豪華-混帳使用命令: Install-Module posh-git
爲了保證豪華-git的負載對每個外殼,使用Add-PoshGitToPrompt
command。
@ Paul-
GIT中我的PowerShell配置文件是基於關閉的腳本,我發現這裏的:
http://techblogging.wordpress.com/2008/10/12/displaying-git-branch-on-your-powershell-prompt/
我已經修改了它一下,以顯示目錄路徑和位格式化。由於我使用PortableGit,它還設置了我的Git bin位置的路徑。
# 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:\ Projects \ forks \ fluent-nhibernate>
*注意:用JakubNarębski的建議更新。
乾杯大衛,我ewas很容易能夠修改此,並使用來自鏈接的博客帖子的指示,以獲得適合我的東西啓動和運行。 – 2009-08-17 14:31:54
不要刮git分支輸出獲取當前分支的名稱;它意味着最終用戶(它是瓷器)。使用'git symbolic-ref HEAD'。不要使用git-status;它用於最終用戶,並且可能會發生變化(它將在1.7.0中更改)。使用git-diff-files,git-diff-tree,git-diff-index。 – 2009-08-17 19:54:14
#JakubNarębski - 根據您的建議更新了代碼 - 非常感謝。由於它沒有調用分支和狀態,所以速度也相當快。 – 2009-08-18 03:20:23
我調整了提示代碼(來自@ david-longnecker答案),使其更加豐富多彩。
這裏是我的代碼
Function Prompt {
Write-Host("")
Remove-Variable status_string
Remove-Variable branch
Remove-Variable localchanges
$symbolicref = git symbolic-ref HEAD
if($symbolicref -ne $NULL) {
$status_string += "GIT"
$branch = $symbolicref.substring($symbolicref.LastIndexOf("/") +1)
$differences = (git diff-index --name-status HEAD)
If ($differences -ne $NULL) {
$localchanges = $true
$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 {
$localchanges = $false
$git_create_count = 0
$git_update_count = 0
$git_delete_count = 0
}
#$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) -nonewline -foregroundcolor White
#Output the branch in prettier colors
Write-Host (" [") -nonewline -foregroundcolor White
If ($branch -ne "master") {Write-Host ($branch) -nonewline -foregroundcolor Red}
else {Write-Host ($branch) -nonewline -foregroundcolor DarkYellow}
Write-Host ("] ") -nonewline -foregroundcolor White
#Output changes count, if any, in pretty colors
If ($localchanges) {
Write-Host ("c:") -nonewline -foregroundcolor White
If ($git_create_count -gt 0) {Write-Host ($git_create_count) -nonewline -foregroundcolor Green}
else {Write-Host ($git_create_count) -nonewline -foregroundcolor White}
Write-Host (" u:") -nonewline -foregroundcolor White
If ($git_update_count -gt 0) {Write-Host ($git_update_count) -nonewline -foregroundcolor Yellow}
else {Write-Host ($git_update_count) -nonewline -foregroundcolor White}
Write-Host (" d:") -nonewline -foregroundcolor White
If ($git_delete_count -gt 0) {Write-Host ($git_delete_count) -nonewline -foregroundcolor Red}
else {Write-Host ($git_delete_count + " ") -nonewline -foregroundcolor White}
}
#Output the normal prompt details, namely the path
Write-Host ("| " + $((get-location).Path) + ">") -nonewline -foregroundcolor White
}
else {
Write-Host ($status_string + $((get-location).Path) + ">") -nonewline -foregroundcolor White
}
return " "}
結果:
下面是結果的命令來查看它會是什麼樣子:
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"
以下是一張就可以了。我已經編輯了一些顏色,使其更具可讀性。
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:
整潔!只是改變了我的背景顏色。你的背景顏色是什麼? – 2017-06-26 13:52:13
@YogeeshSeralathan我對ConEmu使用Monokai主題。背景色是#272822 – tamj0rd2 2017-06-28 17:43:16
如何在啓動時啓用此模塊?看起來像Powershell每次打開一個新實例時都會忘記這個模塊。 – Mihir 2017-10-17 06:31:53
@Mhhir你必須創建一個配置文件,看看答案https://stackoverflow.com/questions/24914589/how-to-create-permanent-powershell-aliases# – 2017-10-19 13:50:51
@NicolaPeluchetti謝謝!我遵循https://www.howtogeek.com/50236/customizing-your-powershell-profile/並在PowerShell配置文件中添加了「Import-Module posh-git」。像魅力一樣工作! – Mihir 2017-10-24 07:00:40