Watch for file changes and add changes to github?

On OSX I want to create a script that 'when a file changes, automatically runs

git add [FILES CHANGED]; 
git commit -m "Changing ${file names}"; 
git push -u origin master"

I understand there is fswatch, however, this keeps a process running.

Maybe I can try running "git status" and if != "On branch master nothing to commit, working directory clean" then automatically push to Github?

Answers

I know this is an old thread, but I was also looking for an alternative solution and found rerun2. Perhaps it will also be useful to somebody else.

https://github.com/tartley/rerun2

From Tartley's readme:

It runs COMMAND every time there's a filesystem modify event within your current directory (recursive). For example, it can re-run tests every time you hit 'save' in your editor. Or it can re-generate rendered output (html, SVG, etc) every time you save the source (e.g. Markdown or GraphViz dot).

COMMAND needs to be a single parameter to rerun, hence if it contains spaces, either quote the whole thing:

rerun "ls ~"

or escape them:

rerun ls\ ~

How to implement:

If you take your code, and put it inside an update.sh script:

#!/usr/bin/env bash
git add -u
git commit -m "Automatic update of modified files" && git push

Make it executable:

chmod u+x gitpush.sh

Create an alias to make things easier:

alias gitpush=/path/to/gitpush.sh:

Open the terminal, got to the directory you want to watch and run the command:

rerun gitpush

Now it will execute gitpush everytime files changes in that directory.

Posted on by Paul van Jaarsveld

If you're willing to give up automatically responding to file modifications, you could just periodically run (e.g., via a crontab entry or something):

git add -u
git commit -m "Automatic update of modified files" && git push

The git add -u will stage any modified files, and "git commit" will only be successful if there are modifications to commit.

If you also want to pick up new files, you could use git add -A instead.

Posted on by larsks