3

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.

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! This worked, but only after I found git.exe buried away in the following folder: C:\Users\crclayton\AppData\Local\GitHub\PortableGit_c2ba306e536fdf878271f7fe636a147ff37326ad\bin.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.