4

Git - Git 在 PowerShell 中使用 Git

 3 years ago
source link: https://git-scm.com/book/zh/v2/%E9%99%84%E5%BD%95-A%3A-%E5%9C%A8%E5%85%B6%E5%AE%83%E7%8E%AF%E5%A2%83%E4%B8%AD%E4%BD%BF%E7%94%A8-Git-Git-%E5%9C%A8-PowerShell-%E4%B8%AD%E4%BD%BF%E7%94%A8-Git
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Git 在 PowerShell 中使用 Git

Windows 中早期的命令行终端 cmd.exe 无法自定义 Git 使用体验,但是如果你正在使用 Powershell,那么你就十分幸运了。 这种方法同样适用于 Linux 或 macOS 上运行的 PowerShell Core。 一个名为 Posh-Git (https://github.com/dahlbyk/posh-git) 的扩展包提供了强大的 tab 补全功能, 并针对提示符进行了增强,以帮助你聚焦于你的仓库状态。 它看起来像:

附带了 Posh-Git 扩展包的 Powershell
Figure 166. 附带了 Posh-Git 扩展包的 Powershell。

前提需求(仅限 Windows)

在可以运行 PowerShell 脚本之前,你需要将本地的 ExecutionPolicy 设置为 RemoteSigned (可以说是允许除了 Undefined 和 Restricted 之外的任何内容)。如果你选择了 AllSigned 而非 RemoteSigned,那么你的本地脚本还需要数字签名后才能执行。如果设置为 RemoteSigned, 那么只有“ZoneIdentifier”设置为 Internet,即从 Web 上下载的脚本才需要签名,其它则不需要。 如果你是管理员,想要为本机上的所有用户设置它,请使用“-Scope LocalMachine”。 如果你是没有管理权限的普通用户,可使用“-Scope CurrentUser”来只为自己设置它。

> Set-ExecutionPolicy -Scope LocalMachine -ExecutionPolicy RemoteSigned -Force

PowerShell Gallery

如果你有 PowerShell 5 以上或安装了 PackageManagement 的 PowerShell 4,那么可以用包管理器来安装 posh-git。

有关 PowerShell Gallery 的更多详情: https://docs.microsoft.com/en-us/powershell/gallery/overview

> Install-Module posh-git -Scope CurrentUser -Force
> Install-Module posh-git -Scope CurrentUser -AllowPrerelease -Force # 带有 PowerShell Core 支持的更新的 beta 版

如果你想为所有的用户安装 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 命令, 它会在你的 $profile 脚本中添加导入语句。此脚本会在每次打开新的 PowerShell 终端时执行。 注意,存在多个 $profile 脚本。例如,其中一个是控制台的,另一个则属于 ISE。

> Import-Module posh-git
> Add-PoshGitToProfile -AllHosts

从源码安装

只需从 (https://github.com/dahlbyk/posh-git) 下载一份 Posh-Git 的发行版并解压即可。 接着使用 posh-git.psd1 文件的完整路径导入此模块:

> Import-Module <path-to-uncompress-folder>\src\posh-git.psd1
> Add-PoshGitToProfile -AllHosts

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK