Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I want to run a powershell script to push to git automatically every so often.

I have GitHub installed for windows. If I run "Git Shell", a powershell shell opens but it has the git function installed and starts in C:\Users\crclayton\Desktop\GitHub> not in H:\>

If I try to use git in a normal PowerShell, it tells me git isn't recognized as a function. However, if I load the script C:\Users\crclayton\AppData\Local\GitHub\shell.ps1 in the normal powershell, I can use git, but when I navigate to the folder and try to add or commit or push, I get the error:

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

This is my code:

#(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

Can anyone see what's wrong or suggest an alternative? Thanks in advance.

like image 343
Charles Clayton Avatar asked May 15 '26 21:05

Charles Clayton


2 Answers

If you want to use git commands inside a powershell script make sure you have a record in the environment variable 'Path' with the actual path to the git. In my case it looks as shown on the attached picture!:

Path environment variable

Anyway, this setting was automatically added when I installed Git Extensions. If you don't use extensions just add this line to the 'Path' variable eather manually or from powershell script.

Hope this will help

like image 51
Volodymyr Daiev Avatar answered May 17 '26 09:05

Volodymyr Daiev


Github for windows uses posh-git so you may want to confirm that you are following the manual install to be used by 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.

like image 32
talves Avatar answered May 17 '26 10:05

talves



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!