使用PowerShell尝试提交到GitHub时出现错误“致命错误: 无法访问'Microsoft.PowerShell.Core\FileSystem”。

3

我想运行一个powershell脚本,以便定时自动推送到git。

我已经安装了GitHub for Windows。如果我运行“Git Shell”,则会打开一个powershell shell,但它已安装git函数,并且从C:\Users\crclayton\Desktop\GitHub>开始而不是H:\>

如果我尝试在普通的PowerShell中使用git,它会告诉我未能识别git作为函数。然而,如果我在普通powershell中加载脚本C:\Users\crclayton\AppData\Local\GitHub\shell.ps1,那么就可以使用git,但当我导航到文件夹并尝试addcommitpush时,我会得到以下错误:

git.exe:致命错误:无法访问

'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

有人能看到问题在哪里或者提出替代方案吗?先谢谢了。

2个回答

2
如果您想在PowerShell脚本中使用git命令,请确保在环境变量“Path”中记录了实际的git路径。在我的情况下,它看起来如附图所示!无论如何,当我安装Git扩展时,此设置会自动添加。如果您不使用扩展程序,只需将此行添加到“Path”变量中,可以手动或从PowerShell脚本中执行。希望这可以帮助到您。

谢谢!这个方法可行,但是我只有在找到 git.exe 藏在以下文件夹中后才成功:C:\Users\crclayton\AppData\Local\GitHub\PortableGit_c2ba306e536fdf878271f7fe636a147ff37326ad\bin - Charles Clayton

1
Github for Windows使用posh-git,因此您可能希望确认您正在按照手动安装指南来使用powershell。

使用Get-ExecutionPolicy验证允许执行脚本(应为RemoteSignedUnrestricted)。如果未启用脚本,请以管理员身份运行PowerShell,并调用Set-ExecutionPolicy RemoteSigned -Scope CurrentUser -Confirm


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