-
A1. 附录 A:其他环境下的 Git
- A1.1 图形界面
- A1.2 Visual Studio 中的 Git
- A1.3 Visual Studio Code 中的 Git
- A1.4 IntelliJ / PyCharm / WebStorm / PhpStorm / RubyMine 中的 Git
- A1.5 Sublime Text 中的 Git
- A1.6 Bash 中的 Git
- A1.7 Zsh 中的 Git
- A1.8 PowerShell 中的 Git
- A1.9 总结
-
A2. 附录 B:在应用程序中嵌入 Git
-
A3. 附录 C:Git 命令
A1.8 附录 A:其他环境下的 Git - PowerShell 中的 Git
PowerShell 中的 Git
Windows 上传统的命令行终端 (cmd.exe
) 实际上无法提供自定义的 Git 体验,但是如果您使用的是 PowerShell,那么您很幸运。 这也适用于在 Linux 或 macOS 上运行 PowerShell Core 的情况。 一个名为 posh-git 的软件包 (https://github.com/dahlbyk/posh-git) 提供了强大的 Tab 键补全功能,以及增强的提示符,可帮助您掌握存储库的状态。 看起来像这样

安装
前提条件(仅限 Windows)
在您的计算机上运行 PowerShell 脚本之前,您需要将本地 ExecutionPolicy
设置为 RemoteSigned
(基本上,除了 Undefined
和 Restricted
之外的任何值)。 如果您选择 AllSigned
而不是 RemoteSigned
,那么也需要对本地脚本(您自己的脚本)进行数字签名才能执行。 使用 RemoteSigned
,只有 ZoneIdentifier
设置为 Internet
的脚本(从 Web 下载的脚本)才需要签名,其他的则不需要。 如果您是管理员,并且想要为该计算机上的所有用户进行设置,请使用 -Scope LocalMachine
。 如果您是普通用户,没有管理权限,则可以使用 -Scope CurrentUser
仅为您设置。
更多关于 PowerShell 作用域的信息:https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_scopes。
更多关于 PowerShell ExecutionPolicy 的信息:https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.security/set-executionpolicy。
要为所有用户将 ExecutionPolicy
的值设置为 RemoteSigned
,请使用以下命令
> Set-ExecutionPolicy -Scope LocalMachine -ExecutionPolicy RemoteSigned -Force
PowerShell 库
如果你至少拥有 PowerShell 5 或已安装 PackageManagement 的 PowerShell 4,你可以使用包管理器为你安装 posh-git。
有关 PowerShell Gallery 的更多信息:https://learn.microsoft.com/en-us/powershell/scripting/gallery/overview。
> Install-Module posh-git -Scope CurrentUser -Force
> Install-Module posh-git -Scope CurrentUser -AllowPrerelease -Force # Newer beta version with PowerShell Core support
如果你想为所有用户安装 posh-git,请改用 -Scope AllUsers
并从提升的 PowerShell 控制台中执行该命令。如果第二个命令失败,并出现类似 Module 'PowerShellGet' was not installed by using Install-Module
的错误,你将需要先运行另一个命令
> Install-Module PowerShellGet -Force -SkipPublisherCheck
然后你可以返回并重试。发生这种情况是因为 Windows PowerShell 附带的模块使用不同的发布证书签名。
更新 PowerShell 提示符
要在提示符中包含 Git 信息,需要导入 posh-git 模块。 要在每次 PowerShell 启动时都导入 posh-git,请执行 Add-PoshGitToProfile
命令,该命令会将 import 语句添加到你的 $profile
脚本中。 每次打开新的 PowerShell 控制台时,都会执行此脚本。 请记住,存在多个 $profile
脚本。 例如,一个用于控制台,另一个用于 ISE。
> Import-Module posh-git
> Add-PoshGitToProfile -AllHosts
来自源代码
只需从 https://github.com/dahlbyk/posh-git/releases 下载 posh-git 发布版本,并解压缩它。 然后使用 posh-git.psd1
文件的完整路径导入模块
> Import-Module <path-to-uncompress-folder>\src\posh-git.psd1
> Add-PoshGitToProfile -AllHosts
这会将正确的行添加到你的 profile.ps1
文件中,并且 posh-git 将在你下次打开 PowerShell 时处于活动状态。
有关提示符中显示的 Git 状态摘要信息的描述,请参见:https://github.com/dahlbyk/posh-git/blob/master/README.md#git-status-summary-information 有关如何自定义 posh-git 提示符的更多详细信息,请参见:https://github.com/dahlbyk/posh-git/blob/master/README.md#customization-variables。