简体   繁体   中英

How do I get the Visual Studio “Open Command Line” to open the Git Shell installed by GitHub for Windows?

You need to get powershell to run a couple of scripts on startup to initialise the shell in the same way as clicking the "Git Shell" shortcut installed by GitHub for Windows. You can do this via the -command command-line argument supplied to powershell.exe

This can be done as follows:

Open Tools > Options > Environment / Command Line

Set Command to: powershell.exe

Set Command Arguments to the following (single line):

-ExecutionPolicy Bypass -NoExit -Command .(Resolve-Path "$env:LOCALAPPDATA\\GitHub\\shell.ps1"); .(Resolve-Path "$env:github_posh_git\\profile.example.ps1")

Remember: Github installs posh-git for you. You have the option to run the posh-git on every shell. You would just setup your default ps1 for the Environment.

Disclaimer:

Make sure you have minimum requirements for posh-git

  • specifically Get-ExecutionPolicy

The following solution was tested on x64 - Win7 and Win8.1

You will be setting up your files in:

PS> cd $env:USERPROFILE\Documents\WindowsPowerShell

To run a Git Shell on each Powershell environment

Create a file Microsoft.PowerShell_profile.ps1 In $env:USERPROFILE\\Documents\\WindowsPowerShell dir

With contents: (Make sure the path to poshgit is correct)

# Load posh-git example profile
. 'C:\tools\poshgit\dahlbyk-posh-git-869d4c5\profile.example.ps1'

Rename-Item Function:\Prompt PoshGitPrompt -Force
function Prompt() {if(Test-Path Function:\PrePoshGitPrompt){++$global:poshScope; New-Item function:\script:Write-host -value "param([object] `$object, `$backgroundColor, `$foregroundColor, [switch] `$nonewline) " -Force | Out-Null;$private:p = PrePoshGitPrompt; if(--$global:poshScope -eq 0) {Remove-Item function:\Write-Host -Force}}PoshGitPrompt}

Make sure the GitHub.PowerShell_profile.ps1 is also in $env:USERPROFILE\\Documents\\WindowsPowerShell

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

shell command (x86)

%SystemRoot%\syswow64\WindowsPowerShell\v1.0\powershell.exe

shell command (x64)

%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe

Command to check x64 vs x86

PS> [Environment]::Is64BitProcess

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM