2021-09-14
Git - Autocommit on File Changes
Motivation: I wanted to track the history of the changes introduced in my notes folder. Didn't want to create a commit, or redact the commit message after each change because typically during the day I made multiple edits in notes ad prefer to minimize time spent on managing my notes. I'm quickly jumping into the notes, adding something to existing notes, or creating new ones. Remembering to commit each change is too much overhead for me. I started to look for a solution that will monitor my notes folder, and, if anything changed, create a commit. I found several solutions that serve that purpose.
For reference see making-git-auto-commit on StackOverflow.
My choice was gitwatch used as systemd service.
- gitwatch , active
- Notes for installation on Mac
- gwatch low activity
- etckeeper
- git-etc github: ★4, low activity, created 9 years ago, last commit in 2014
- Git Powercommit
- git-wip
- inotify
- watchman
- inotify.sh
gitwatch , active
A bash script to watch a file or folder and commit changes to a git repo
Justification for my choice:
- active with large community
- There is an AUR package for Archlinux
- does exactly what I want, nothing more
- can be used as daemon that starts on boot
Usage:
gitwatch [-s <secs>] [-d <fmt>] [-r <remote> [-b <branch>]]
[-m <msg>] [-l|-L <lines>] <target>
Notes for installation on Mac
If running on OS X, you'll need to install the following Homebrew tools:
brew install fswatch
brew install coreutils
and
brew install gitwatch
- If installed to a path other than
/usr/bin/gitwatch
, modifygitwatch@.service
to suit. In my case,gitwatch
was found in/usr/local/bin/gitwatch
-
Create dir if it does not exist and copy systemd service file with
mkdir -p "$HOME/.config/systemd/user" && cp gitwatch@.service $HOME/.config/systemd/user
If you cannot findgitwatch@.service
file take it from the GitHub repository. -
Start and enable the service for a given path by running
systemctl --user --now enable gitwatch@$(systemd-escape "'-r url/to/repository' /path/to/folder").service
On macOS you need to act differently -
take
.plist
file from the repo, and adjust it to your needs - copy modified plist to `/Library/LaunchDaemons/`` with sudo
- note: if there is a proper
gitwatch
script path (I needed to changegitwatch.sh
togitwatch
)
gwatch low activity
A program that watches a folder for file modifications and commits them to a git repository automatically
After gwatch
is started it will watch a given folder and all of its subfolders (recursively) for changes. If a change occurs, a timer will be started (the 30s by default). After the timer expires, gwatch
will create a new git commit with all the modifications. The timer is to prevent creating too many commits when there are a lot of modifications. In order for gwatch
to successfully create commits, a git repository must be initialized in the watched folder.
etckeeper
NOTE: In my case, I didn't need to track permissions
etckeeper
is a collection of tools to let /etc
be stored in a git, mercurial, darcs, or bzr repository. It hooks into apt (and other package managers including yum and pacman-g2) to automatically commit changes made to /etc
during package upgrades. It tracks file metadata that revision control systems do not normally support, but that is important for /etc
, such as the permissions of /etc/shadow
. It's quite modular and configurable, while also being simple to use if you understand the basics of working with revision control.
etckeeper
now has a website at http://etckeeper.branchable.com/.
git-etc github: ★4, low activity, created 9 years ago, last commit in 2014
A simple daemon that automatically creates a git repository in the given directory and creates a commit at the specified time interval.
Has GUI, has AUR package
Git Powercommit
Commit every changed submodule. Commit every changed regular file or folder, aggregating the files that share a folder into a single commit.
git-wip
git-wip
is a script that will manage Work In Progress (or WIP) branches. WIP branches are mostly thrown away but identify points of development between commits. The intent is to tie this script into your editor so that each time you save your file, the git-wip
script captures that state in git. git-wip
also helps you return to a previous state of development.
inotify
This program is an "inotify cron" system. It consists of a daemon and a table manipulator. You can use it in a similar way as the regular cron. The difference is that the inotify cron handles filesystem events rather than time periods.
watchman
Watchman exists to watch files and record when they change. It can also trigger actions (such as rebuilding assets) when matching files change
inotify.sh
This script uses inotifywatch to monitor file status and trigger commands.
Designed for Ansible playbook, but you can use this for other development purposes.
To install e.g. apt-get install -y inotify-tools
see: https://gist.github.com/darencard/5d42319abcb6ec32bebf6a00ecf99e86
]