Skip to content

Instantly share code, notes, and snippets.

@darencard
Last active May 1, 2024 23:18
Show Gist options
  • Save darencard/5d42319abcb6ec32bebf6a00ecf99e86 to your computer and use it in GitHub Desktop.
Save darencard/5d42319abcb6ec32bebf6a00ecf99e86 to your computer and use it in GitHub Desktop.
Automatic file git commit/push upon change

Please see the most up-to-date version of this protocol on my blog at https://darencard.net/blog/.

Automatically push an updated file whenever it is changed

Linux

  1. Make sure inotify-tools is installed (https://github.com/rvoicilas/inotify-tools)
  2. Configure git as usual
  3. Clone the git repository of interest from github and, if necessary, add file you want to monitor
  4. Allow username/password to be cached so you aren't asked everytime
git config credential.helper store
  1. Open a terminal and navigate, as necessary; issue the following command
# <<branch>> = branch you are pushing to
# <<file>> = file you want to monitor
inotifywait -q -m -e CLOSE_WRITE --format="git commit -m 'auto commit' %w && git push origin <<branch>>" <<file>> | bash
  1. In a separate shell, do whatever you want and when monitored file is updated, it will automatically get committed and pushed (as long as the shell with the inotifywait command is still active)

Mac

  1. Make sure fswatch is installed (https://github.com/emcrisostomo/fswatch)
  2. Configure git as usual
  3. Clone the git repository of interest from github and, if necessary, add file you want to monitor
  4. Allow username/password to be cached so you aren't asked everytime
git config credential.helper store
  1. Create a script that performs the commit and push (auto_commit_push.sh)
#!/bin/bash
# <<branch>> = branch you are pushing to
git commit -m "auto commit" $1
git push origin <<branch>>
  1. Open a terminal and navigate, as necessary; issue the following command
# <<file>> = file you want to monitor
# <<path/to/auto_commit_push.sh>> = path to the script created above
fswatch -0 <<file>> | xargs -0 -n 1 bash <<path/to/auto_commit_push.sh>>
  1. In a separate shell, do whatever you want and when monitored file is updated, it will automatically get committed and pushed (as long as the shell with the fswatch command is still active)
@jackseceng
Copy link

@puckvg try the screen command:

Screen

Screen -x "whatever command you want"

While on screen when you want leave things running:

Ctrl+A+D

Leaves process running and returns you to main bash session, you can then logout and process will continue to run.

Hope that helps :)

@MichaelGriebe
Copy link

I had a little trouble with the command -in particular using the %w variable. My file name had spaces, so when it came time to commit the quotation characters got lost in the mix? Anyway, replacing %w with the quoted file name worked.

@isaacisnotjonah
Copy link

For the mac auto_commit_push.sh, $1 should be in quotes (i.e. "$1"), to account for path names that contain spaces.

@Bitcents
Copy link

For people who are interested in getting this to run on Windows, try using WSL or Gitbash. You might have to make certain modifications to the script above, but it should not be too difficult. Happy coding 😎

@Eds-Dbug
Copy link

It only runs once for me, doesnt run a second time

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment