git不在PowerShell中运行

Low*_*ool 1 git powershell

我在powershell中遇到git的问题,因此我 在安装后通过快捷方式打开(如其说明所述),然后将其卸载并使用此官方文章重新安装。但是,如果我打开普通的Powershell,我运行了git命令,它正在考虑将git作为一个文件并问我how do you want to open it?我不明白那里发生了什么?

Cli*_*ers 5

正如您在评论中告诉我们的那样,Get-Command git返回:

CommandType Name Version Source
----------- ---- ------- ------
Application git  0.0.0.0 C:\WINDOWS\system32\git
Run Code Online (Sandbox Code Playgroud)

这表明,您的system32文件夹包含一个名为git(无扩展名)的文件。作为system32您的一部分%PATH%git在PowerShell中键入指向%WINDIR%\system32\git,并且由于它没有扩展名,Windows不知道如何处理它并询问您。

为什么会这样?

通常,这种情况发生在一个人想要通过在cmd中使用错误的管道运算符将一个命令的输出管道传输到另一个命令的时候 someCommand > git

要解决此问题,只需git从system32中删除并确保您%PATH%$env:Path在PowerShell中)变量指向git安装目录。

如果没有,将git安装添加到%PATH%

  • 无论是临时的打字$env:Path += ";C:\path\to\git"
  • 永久
    • 使用系统设置(如其他答案中已描述)
    • 使用PowerShell:
      [Environment]::SetEnvironmentVariable("Path", $env:Path + ";C:\path\to\git", [EnvironmentVariableTarget]::Machine)Machine可以替换User