尝试使用 powershell 提交 GitHub 导致错误 "fatal: unable to access 'Microsoft.PowerShell.Core\FileSystem"

Trying to commit to GitHub using powershell results in error "fatal: unable to access 'Microsoft.PowerShell.Core\FileSystem"

我想 运行 一个 powershell 脚本每隔一段时间自动推送到 git。

我为 windows 安装了 GitHub。如果我 运行 “Git Shell”,电源 shell shell 打开但它安装了 git 功能并在 C:\Users\crclayton\Desktop\GitHub> 不在 H:\>

如果我尝试在普通 PowerShell 中使用 git,它会告诉我 git 未被识别为函数。但是,如果我以正常功率 shell 加载脚本 C:\Users\crclayton\AppData\Local\GitHub\shell.ps1,我可以使用 git,但是当我导航到文件夹并尝试 addcommitpush,我得到错误:

git.exe : fatal: unable to access

'Microsoft.PowerShell.Core\FileSystem::\mypath../.config/git/config': Invalid argument At

C:\Users\crclayton\Desktop\autogit.ps1:7 char:4

  • git <<<< add *

  • CategoryInfo : NotSpecified: (fatal: unable t...nvalid argument: String) [], RemoteException

  • FullyQualifiedErrorId : NativeCommandError

这是我的代码:

#(Resolve-Path "$env:LOCALAPPDATA\GitHub\shell.ps1") # tried this, doesn't work
C:\Users\crclayton\AppData\Local\GitHub\shell.ps1

$msg = Read-Host "Enter commit msg"

cd "C:\Users\crclayton\project\"
git add *
git commit -a -m $msg
git push origin master

任何人都可以看出问题所在或提出替代方案吗?提前致谢。

Github for windows 使用 posh-git 所以你可能想确认你是 following the manual install powershell.

Verify execution of scripts is allowed with Get-ExecutionPolicy (should be RemoteSigned or Unrestricted). If scripts are not enabled, run PowerShell as Administrator and call Set-ExecutionPolicy RemoteSigned -Scope CurrentUser -Confirm.

如果您想在 powershell 脚本中使用 git 命令,请确保您在环境变量 'Path' 中有一条记录,其中包含 git 的实际路径。 在我的情况下,它看起来如附图所示!:

无论如何,这个设置是在我安装 Git 扩展程序时自动添加的。如果您不使用扩展,只需手动或从 powershell 脚本将此行添加到 'Path' 变量 eather。

希望这会有所帮助