在Windows PowerShell上使用Git

3

我想在PowerShell中使用git,但遇到了问题。 我正在按照Miguel Grinberg的pycon研讨会上的步骤进行克隆,他使用git clone“”。 我知道我可以下载git,并且可以通过git bash完成它。但我想在Windows PowerShell上执行该命令。 除了gitbash之外,是否可以完成这个操作?


你刚刚在安装Git后,在Git Bash之外尝试过它吗? - poke
3个回答

1
如果你从https://git-scm.com/download/win下载了git,那么在安装时你可以选择是否修改PATH。

Git Setup

如果您选择“仅从Git Bash使用Git”,除非您手动将X:\path-to-git-installation\cmd添加到PATH中,否则您将无法在powershell中使用它。
否则,您在powershell中使用git不应该有任何问题。

0
如果您下载并安装Windows上的git客户端,它将在powershell中正常工作。

https://git-scm.com/download/win

例如:

PS C:\Users\joeyoung> git --version
git version 1.9.5.msysgit.1
PS C:\Users\joeyoung> git clone https://github.com/miguelgrinberg/flask-pycon2015.git
Cloning into 'flask-pycon2015'...
remote: Counting objects: 70, done.
remote: Total 70 (delta 0), reused 0 (delta 0), pack-reused 70
Unpacking objects: 100% (70/70), done.
Checking connectivity... done.
PS C:\Users\joeyoung> dir .\flask-pycon2015


    Directory: C:\Users\joeyoung\flask-pycon2015


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----        31/08/2015     10:56            templates
-a---        31/08/2015     10:56        675 .gitignore
-a---        31/08/2015     10:56       3735 app.py
-a---        31/08/2015     10:56       3629 guess.py
-a---        31/08/2015     10:56       1083 LICENSE
-a---        31/08/2015     10:56         99 README.md
-a---        31/08/2015     10:56        112 requirements.txt


PS C:\Users\joeyoung>

0

由于我只需要Git.exe,所以我将便携式Git提取到一个文件夹中,并将以下内容添加到我的Powershell配置文件中...

# Use the locally installed Git, if available.
$gitExecutable = Get-Command git.exe -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Source
if ($gitExecutable -eq $null) {
    $gitExecutable = "$env:USERPROFILE\bin\PortableGit\bin\git.exe"
}

function Invoke-Git {
<#
.Synopsis
Wrapper function that deals with Powershell's peculiar error output when Git uses the error stream.

.Example
Invoke-Git ThrowError
$LASTEXITCODE

#>
    [CmdletBinding()]
    param(
        [parameter(ValueFromRemainingArguments=$true)]
        [string[]]$Arguments
    )

    & {
        [CmdletBinding()]
        param(
            [parameter(ValueFromRemainingArguments=$true)]
            [string[]]$InnerArgs
        )
        &$gitExecutable $InnerArgs
    } -ErrorAction SilentlyContinue -ErrorVariable fail @Arguments

    if ($fail) {
        $fail.Exception
    }

}

Set-Alias -Name git -Value Invoke-Git
Set-Alias -Name git.exe -Value Invoke-Git

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接