Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows .bat file to schedule git add, commit and push to Github

I need to schedule a task in Windows Server 2008 R2 to automatically push files from a folder to Github Enterprise (I don't think the "Enterprise" part will make it different than regular Github, except that ssh authentication is required).

Here's what I've done:

  1. Set up a public/private key pair and added them to Github and .ssh, respectively
  2. Created a Github repo and cloned it to a local folder
  3. Manually ran through the add, commit, and push steps to make sure it works that way
  4. Tried to write a .bat file for automatically doing step #3
  5. Scheduled the task to run as my user, with highest privileges, in the target folder

Unfortunately, the task fails because I am not writing the .bat file correctly.

This was my attempt at the .bat file:

echo git add . | "C:\Program Files\Git\bin\git.exe" 
echo git commit -m 'scheduled commit' | "C:\Program Files\Git\bin\git.exe" 
echo git push | "C:\Program Files\Git\bin\git.exe" 

I found a good resource on how to do this, but it is unfortunately very Linux-specific.

like image 855
Hack-R Avatar asked Dec 13 '16 18:12

Hack-R


2 Answers

Just do:

git add . 
git commit -m 'scheduled commit'
git push

Or, if git is not in your path:

"C:\Program Files\Git\bin\git.exe" add .
"C:\Program Files\Git\bin\git.exe" commit -m 'scheduled commit'
"C:\Program Files\Git\bin\git.exe" push

And depending on what you want to achieve,

git commit -a -m 'scheduled commit'

to commit only tracked file that changed, or :

git add -A
git commit -m 'scheduled commit'

to commit new file or delete is even better...

like image 189
Philippe Avatar answered Oct 08 '22 13:10

Philippe


The above answer from Philippe works. However, it does not work if it is run somewhere the errorlevel is checked after the execution of commit (like in Jenkins). A small modification solves that:

git add -A
git diff-index --quiet HEAD || git commit -m "scheduled commit"

This is explained further in the original answer on https://devops.stackexchange.com/a/5443

like image 45
Mats Bengtsson Avatar answered Oct 08 '22 14:10

Mats Bengtsson