如何从自定义 shell 使用 GitHub for Windows 附带的 posh-git?

Posted

技术标签:

【中文标题】如何从自定义 shell 使用 GitHub for Windows 附带的 posh-git?【英文标题】:How to use posh-git that comes with GitHub for Windows from custom shell? 【发布时间】:2012-09-12 08:21:58 【问题描述】:

安装适用于 Windows 的 GitHub 是立即获得初学者的 git Windows 应用程序 msysgit 和 posh-git 的好方法 - 但您只能通过调用“GitHub.appref-ms --open-”来使用 posh-git 启动 git shell壳”。

我想做的是启动控制台,加载 powershell,并使用 Github for Windows 附带的 posh-git - 但我想不出将其加载到 powershell 中的方法。

更新:我不想单独安装 Git for Windows,因为 msysgit 已经安装。

通过调用 AppData\Local\Github 文件夹中的 shell1.ps1 脚本,我取得了一些进展——它确实让我调用了 git。但它不显示我所在的分支,与 GitHub for Windows 的“Git Shell”相反。

【问题讨论】:

此链接有帮助吗? haacked.com/archive/2011/12/19/get-git-for-windows.aspx @Christian - 不,查看更新的问题 【参考方案1】:

虽然@dahlbyk 的回答没有提供解决方案,但它仍然把我推向了正确的方向。问题是,Phil 的 Haack 博客文章中引用的 $env:posh-git 环境未定义,因此在 PowerShell 配置文件脚本中对其进行测试没有任何作用。

但有效的是从同一个脚本运行 shell.ps1 和 profile.example.ps1。这是像我这样的 PowerShell 菜鸟的秘诀:

    在活动用户的 Documents\WindowsPowerShell 文件夹中编辑或创建 Microsoft.PowerShell_profile.ps1(如果该文件夹不存在,则创建该文件夹)

    添加以下两行:

    . (Resolve-Path "$env:LOCALAPPDATA\GitHub\shell.ps1")
    . $env:github_posh_git\profile.example.ps1
    

【讨论】:

有没有办法阻止它强制我登录每个命令? 这种方法也记录在 Git Docs git-scm.com/book/en/v2/… 这里是任何想要 Posh-Git 的 Console2 / ConsoleZ 选项卡的人的快捷方式。将选项卡的 Shell 属性设置为:%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -NoExit -Command "& . (Resolve-Path "$env:LOCALAPPDATA\GitHub\shell.ps1"); . $env:github_posh_git\profile.example.ps1;"【参考方案2】:

来自http://haacked.com/archive/2012/05/21/introducing-github-for-windows.aspx,将其放入您的个人资料中以从 GitHub for Windows 加载 posh-git:

# If Posh-Git environment is defined, load it.
if (test-path env:posh_git) 
    . $env:posh_git

【讨论】:

【参考方案3】:

Following these directions,要在 powershell 中安装和使用 Posh-git,请执行以下操作:

    git clone git://github.com/dahlbyk/posh-git.git 到您选择的目录 允许使用以下任一方式执行脚本:
      Set-ExecutionPolicy RemoteSigned Set-ExecutionPolicy Unrestricted
    cd posh-git .\install.ps1 . $PROFILE

我最初遵循流行的 haacked.com 教程中给出的指导(正如 @dahlbyk 和 @CB 所指出的那样),但遇到了与 OP 类似的问题。

【讨论】:

以上是关于如何从自定义 shell 使用 GitHub for Windows 附带的 posh-git?的主要内容,如果未能解决你的问题,请参考以下文章

text 从自定义Github repo安装Heroku依赖项

如何从自定义函数中调用委托函数?

如何从自定义 maven mojo 创建 maven 配置文件

如何从自定义 UIView 类调用函数?

如何使用angularJS从自定义下拉列表中获取选定的值

如何从自定义 UITextView 单元中获取文本?