My bash-fu is laughable and this took me days (yes, days!) to put together, so hope it helps.
It uses fswatch
which you should have installed and is supposedly cross-platform, but I've only tested it on MacOS (brew install fswatch
).
This is my use case:
- I keep my dotfiles in multiple git repos under
~/Dropbox/dotfiles
. - I symlink them to wherever they need to be.
- I want to edit a file, and, as soon as I save it, have it be added and commited to its repo.
Naming things is hard, and so is "naming" file changes as commit messages. Often I take a piece of bash config from StackOverflow, and I paste the link to the answer as a comment in the file itself, so it'd be redundant to justify the change in a commit message.
Most of all, I don't want to carefully organize my commits for these things, just have a way to undo screw-ups.
- Create a file (let's say,
paths.txt
) with one path per line of repositories you want to watch and auto-commit. - Make sure
git-watch.sh
andgit-commit-unattended.sh
are in your PATH. I symlink them to~/usr/bin/
. - Call
git-watch.sh < /path/to/paths.txt
.
Of course you can use this to track changes to any directory, not only one with dotfiles. I also add ~/Dropbox/org
to my paths.txt
file to keep track of changes to my org-mode files, for instance. It's a life-saver.
This is also not exclusive to Dropbox. It could be a mounted remote filesystem (who uses that?) or a local git repo with a post-commit-hook that pushes it to a remote.
I'm still trying to figure out how to use launchd
on MacOS to start this automatically, but it is proving harder than I thought😓. You'll find below my current (non-working) .plist
file.
Starting it yourself by calling git-watch-my-paths-on-login.sh
works though, so if that's good enough for you, you've got a watcher already!
If this helped you, I'd love to know about it. Makes me want to share more stuff I patch together for myself. A star on the gist or a fave/RT on Twitter means the world to me 😊
Very helpful, thanks! I was trying to modify https://github.com/gitwatch/gitwatch to work on mac, but it's a little over-complicated.