如何在 PowerShell 提示符中显示当前的 git 分支名称?
基本上我在这个之后,但对于PowerShell而不是bash。
我通过 PowerShell 在 Windows 上使用 git。如果可能,我希望将当前的分支名称显示为命令提示符的一部分。
Basically I'm after this but for PowerShell instead of bash.
I use git on windows through PowerShell. If possible, I'd like my current branch name to displayed as part of the command prompt.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
一种更简单的方法是安装 Powershell 模块 posh-git。它开箱即用,并带有所需的提示:
(为了安装 posh-git 我建议使用 psget )
如果您没有 psget,请使用以下命令:
要安装 posh-git,请使用以下命令:
Install-Module posh-git
要确保为每个 shell 加载 posh-git,请使用
Add-PoshGitToProfile
命令。An easier way would be just installing the Powershell module posh-git. It comes out of the box with the desired prompt:
(For installing posh-git I suggest using psget)
If you don't have psget use the following command:
To install posh-git use the command:
Install-Module posh-git
To ensure posh-git loads for every shell, use the
Add-PoshGitToProfile
command.这是我的看法。我对颜色进行了一些编辑,以使其更具可读性。
Microsoft.PowerShell_profile.ps1
示例 1:
示例 2:
Here's my take on it. I've edited the colours a bit to make it more readable.
Microsoft.PowerShell_profile.ps1
Example 1:
Example 2:
@Paul-
我的 Git PowerShell 配置文件基于我在这里找到的脚本:
http://techblogging.wordpress.com/2008/10/12/displaying-git-branch-on-your-powershell-prompt/
我已将其修改为位显示目录路径和一些格式。由于我使用 PortableGit,它还设置了我的 Git bin 位置的路径。
到目前为止,这非常有效。在存储库中时,提示符看起来像这样:
GIT [master] c:0 u:1 d:0 | J:\ Projects \ forks \ Fluent-nhibernate>
*注:根据 Jakub Narębski 的建议进行了更新。
@Paul-
My PowerShell profile for Git is based off of a script I found here:
http://techblogging.wordpress.com/2008/10/12/displaying-git-branch-on-your-powershell-prompt/
I've modified it a bit to display the directory path and a bit of formatting. It also sets the path to my Git bin location since I use PortableGit.
So far, this has worked really well. While in a repo, the prompt happily looks like:
GIT [master] c:0 u:1 d:0 | J:\Projects\forks\fluent-nhibernate>
*NOTE: Updated with suggestions from Jakub Narębski.
posh-git 很慢,有更好的方法 https://ohmyposh.dev/。
ohmyposh
模块:安装支持来自 https://www.nerdfonts.com/。
我喜欢 Meslo LGM NF。
在 powershell 默认设置中设置该字体:
C:\Program Files\PowerShell\7
打开/创建文件Microsoft.PowerShell_profile.ps1
并写入下面设置主题(与屏幕截图相同):您也可以选择其他主题。通过运行
Get-PoshThemes
查看预览现在在包含 git repo 的位置打开 powershell,您将看到状态。
查看更多信息:启动您的 PowerShell
posh-git is slow, there's a better way with https://ohmyposh.dev/.
ohmyposh
module:install font that supports glyphs (icons) from https://www.nerdfonts.com/.
I like Meslo LGM NF.
set that font in powershell defaults settings:
Microsoft.PowerShell_profile.ps1
atC:\Program Files\PowerShell\7
and write below to set theme (same as screenshot):You can choose other theme also. see preview by running
Get-PoshThemes
Now open powershell at location containing git repo and you'll see the status.
See more: Power up your PowerShell
使用 Git 2.22(2019 年第 2 季度),任何脚本(无论是否为 Powershell)都可以使用新的
--show-current
选项。如果为空,则表示“分离的 HEAD”。
With Git 2.22 (Q2 2019), any script (Powershell or not) could use the new
--show-current
option.If empty, it means "detached HEAD".
我调整了提示代码(来自@david-longnecker 的回答),使其更加丰富多彩。
编辑: 2019 年 6 月 - 更新以显示未跟踪、隐藏、重命名。调整视觉效果以显示索引。
为什么我使用这个(通过 posh-git 等):
powershell 代码:
注意: 使用的一些命令是瓷器(不建议用于脚本/解析,例如
git status
)。最终将迁移到管道 命令,但这目前有效。结果(VSCode,使用 Powershell 终端):
以下是来自结果的命令,用于查看结果:
I tweaked the prompt code (from @david-longnecker answer) to be a bit more colorful.
Edit: June 2019 - Updated to show untracked, stashes, renames. Tweaked visual to show index.
Why I use this (over posh-git etc):
The powershell code:
NOTE: Some commands used are porcelain (not recommended for scripting/parsing, e.g.
git status
). Will eventually migrate to plumbing commands, but this works for now.The result (VSCode, Using Powershell terminal):
Here are commands from result to view what it would look like:
我改编了 @tamj0rd2 答案,以进一步在存储库的子文件夹中显示分支名称。
查看差异:
I adapted @tamj0rd2 answer to further display the branch name in sub folders of the repository as well.
See the diff:
我喜欢已接受的答案,因此我将详细说明设置它的步骤。
您可以使用 Chocolatey 或使用可用于新 Core PowerShell 的
PowerShellGet
命令来安装 PoshGit。对于 Chocolatey,您需要先安装它才能继续。
在管理员/提升的 shell 中执行命令:
对于 Core PowerShell,还需要安装。要安装 Core PowerShell,请执行以下命令:
安装 Core PowerShell 后,执行以下命令安装 PoshGit:
使用 PoshGit 需要您使用以下命令将其导入到当前运行的 shell 环境中:命令导入模块 posh-git。这意味着每次打开新 shell 时都必须运行此命令。
如果您希望 PoshGit 始终可用,您应该执行以下命令:
I like the accepted answer so I will elaborate on the steps to set it up.
You can install PoshGit using Chocolatey or using the
PowerShellGet
command which is available for the new Core PowerShell.For Chocolatey, you need to have it already installed before proceeding.
In an administrator/elevated shell execute the command:
For Core PowerShell, installation is also required. To install Core PowerShell, execute the following command:
After installing Core PowerShell, execute the command below to install PoshGit:
Using PoshGit requires you import it to the currently running shell environment using the command Import-Module posh-git. This means you have to run this command every time you open a new shell.
If you want PoshGit to be always available you should execute this command instead:
从 @tamj0rd2 的回答中,我们可以将分支名称获取到这样的字符串变量中。
From @tamj0rd2's answer we can get the branch name to a string variable like this.
这是我的 PowerShell Core 配置。只需复制下面的函数并将其放入您的
$PROFILE
Here is my configuration for PowerShell Core. just copy the function below and put it in your
$PROFILE
对于我来说,这是在 Windows 11 上使用终端应用程序的最简单方法。它在主题 PowerShell 上运行,不应用样式,并在子目录中显示分支。
echo %profile
C:\Users\YourName\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
我使用日光深色 PowerShell 主题的最终结果:
This was the simplest method for me on Windows 11 using the Terminal app. It works on a themed PowerShell, applies no styling, and displays the branch when in sub directories.
echo %profile
in powershellC:\Users\YourName\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
My final result with the solarized dark PowerShell theme:
我使用了与上面 @tamj0rd2 类似的方法,但不太复杂。
打开:Microsoft.PowerShell_profile.ps1
添加第一个:
添加第二个:
结果如下:
我是如何通过使用 split-path 来实现减少的字符串或目录路径的,所以总共的提示是:
I used a similar method to the one above from @tamj0rd2, but less complex.
Open: Microsoft.PowerShell_profile.ps1
Add First:
Add Second:
The result is as below:
How I achieved the reduced string or directory path is by using the split-path, so in total the prompt is: