1

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?

user2874945
  • 308
  • 7
  • 16
  • possible duplicate of [Is there a command like "watch" or "inotifywait" on the Mac?](http://stackoverflow.com/questions/1515730/is-there-a-command-like-watch-or-inotifywait-on-the-mac) – hek2mgl Feb 18 '15 at 14:18
  • You need to use `fswatch` if you want to get something stable – hek2mgl Feb 18 '15 at 14:22
  • 1
    @hek2mgl it's not a duplicate. There they asked if there exists a command, but I was hoping for an implementation using fswatch or the like. – user2874945 Feb 18 '15 at 14:53
  • Probably your question should state this more clearly. I retracted the close vote. – hek2mgl Feb 18 '15 at 15:08

2 Answers2

2

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 run gitpush everytime files changes in that directory.

Paul van Jaarsveld
  • 1,524
  • 1
  • 10
  • 9
1

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.

larsks
  • 277,717
  • 41
  • 399
  • 399
  • 1
    What if a file changes two times a minute? – hek2mgl Feb 18 '15 at 14:19
  • There was no requirement stated in the question that said that they needed a separate commit for every individual change. – larsks Feb 18 '15 at 14:21
  • 2
    OP stated `[ a script which ] when a file changes, automatically runs` ... Suggesting `cron` is inappropriate here. `fswatch` needs to be used. – hek2mgl Feb 18 '15 at 14:23
  • Yes, I read that too. You and I are interpreting the question differently, and that's okay. – larsks Feb 18 '15 at 14:24
  • 2
    How can this get misinterpreted? Reacting on file changes is a common task in OS development and I doubt that kernel developers put so much effort on fs notification interfaces to finally end up users using cron. – hek2mgl Feb 18 '15 at 14:26
  • I was hoping for a better solution than running a cron job. Similar to what @hek2mgl said I would ideally prefer a single commit for every file changed. – user2874945 Feb 18 '15 at 14:50