关于git:尝试使用Powershell提交到GitHub会导致错误”致命:无法访问’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",则会打开一个Powershell外壳,但它安装了git函数,并在C:\\Users\\crclayton\\Desktop\\GitHub>中而不是在H:\\>

中启动

如果我尝试在普通的PowerShell中使用git,它会告诉我git无法识别为函数。但是,如果我将脚本C:\\Users\\crclayton\\AppData\\Local\\GitHub\\shell.ps1加载到普通Powershell中,则可以使用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

这是我的代码:

1
2
3
4
5
6
7
8
9
#(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

任何人都可以看到问题所在或提出替代建议吗?预先感谢。


如果要在Powershell脚本中使用git命令,请确保在环境变量\\'Path \\'中有一条记录,其中包含git的实际路径。
就我而言,它看起来如所附图片所示:

Path

无论如何,此设置是在我安装Git Extensions时自动添加的。如果不使用扩展名,只需手动或通过powershell脚本将此行添加到\\'Path \\'变量中即可。

希望这会有所帮助


用于Windows的Github使用posh-git,因此您可能需要确认是否遵循了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.