-
1. 起步
-
2. Git 基础
-
3. Git 分支
-
4. 服务器上的 Git
- 4.1 协议
- 4.2 在服务器上部署 Git
- 4.3 生成 SSH 公钥
- 4.4 架设服务器
- 4.5 Git Daemon
- 4.6 Smart HTTP
- 4.7 GitWeb
- 4.8 GitLab
- 4.9 第三方托管服务
- 4.10 小结
-
5. 分布式 Git
-
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
(从网络下载的)的脚本才需要签名,其他则不需要。如果你是管理员并想为该机器上的所有用户设置,请使用 -Scope LocalMachine
。如果你是普通用户,没有管理权限,则可以使用 -Scope CurrentUser
仅为你自己设置。
关于 PowerShell 作用域的更多信息:https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_scopes。
关于 PowerShell 执行策略的更多信息: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 库的更多信息: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 模块。为了让 posh-git 在每次 PowerShell 启动时都导入,执行 Add-PoshGitToProfile
命令,该命令会将导入语句添加到你的 $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
文件中,下次你打开 PowerShell 时,posh-git 将会激活。
有关提示符中显示的 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。