Skip to content

Instantly share code, notes, and snippets.

@MaisaMilena
Last active June 18, 2020 00:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MaisaMilena/8c551d812873037b633d626dd3a6f760 to your computer and use it in GitHub Desktop.
Save MaisaMilena/8c551d812873037b633d626dd3a6f760 to your computer and use it in GitHub Desktop.

How to setup Gitwatch

Gitwatch is a bash script to watch a file or folder and commit changes to a git repo. I found it while looking for how to automatically commit and push files that changed in a certain folder. Gitwatch is great but I had some trouble finding enough information on how to set it up.

Let's go to the point.

  1. First of all, you will need a git environment configured. The way I recommend to avoid having problems on the next steps is to clone the repository you will be working on using SSH. You can try using HTTP but will probably face the Terminal/Console asking for your Github username and password every time Gitwatch needs to do a push.
  • SSH: git clone git@github.com:user/repo.git
  • HTTPS: git clone https://github.com/user/repo.git
  1. Clone Gitwatch (to install it):
git clone https://github.com/gitwatch/gitwatch.git
cd gitwatch
[sudo] install -b gitwatch.sh /usr/local/bin/gitwatch

Requirements

Ubuntu

If you are using an Ubuntu, you will need to install inotifywait (part of inotify-tools) and have it globally available:

  • sudo apt update
  • apt-get install inotify-tools

MacOS

You'll need to install the following Homebrew tools:

  • brew install fswatch
  • brew install coreutils

Usage

If using SSH

If you are going to use this script on a virtual machine that requires a connection using SSH you will need this extra step. The problem is that when your SSH session ends, the process will also end and that's not what we want. The solution I found was to use tmux (Linux).

  • ssh into the remote machine
  • start tmux by typing tmux into the shell
  • start the process inside the started tmux session (see bellow)
  • leave/detach the tmux session by typing Ctrl+b and then d

Start gitwatch

Go to your repository on the folder you want to watch.

  • gitwatch.sh . &
    This will commit on every change in the current folder (represented by the .). The & at the end of the command is required and sends the Gitwatch process to the background.If you want to watch a file, change the . for the file name. Ex: gitwatch my_file &

  • gitwatch -r <remote> -b <branch_name> . &
    This will commit and push on every change in the current folder (represented by the .). -r is the tag to push on a remote and it's followed by the branch where you want to push (-b branch_name).
    Ex: gitwatch -r origin -b master . &

After running one of the past commands you should receive a number as a response, something like [1] 13597. This is the PID, the number identifying the process that is running Gitwatch script.

After this, the process goes into the main loop (which will run forever, until the script is forcefully stopped/killed). To kill the process and stop the script you can type CMD + C or pkill -15 -P $GITWATCH_PID.

If using tmux, you must check how to access/kill the process.

That's it! By now you should be able to make updates and Gitwatch update your repository.

Final considerations

There are a lot of things you still can do like changing the amount of time between every new commit, the message for the commit, events to be watched, install it as Debian service and others.

For more information, I recommend visiting Gitwatch repository. You can also check some of their examples.

Another good material is Constant Backup Using Gitwatch by Frank Hoffmann.

About using tmux, you can get some good info here.

I hope this could help you.

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