Automatic File History Using GIT

Wed 01 June 2022

I wanted a way to automatically record any file changes in a git workspace.

Add a worktree somewhere other than the cloned directory. 1. cd ~/projects/myrepo 3. git worktree add -f ~/projects/myrepo-sync 4. .git/ must be in .gitignore - Plus add any other directories you do not want to record the history of. e.g. Large files.

Add the contents of this script to a file, and add it to the crontab. (crontab -e) 1. To save every 5 minutes: /5 * * * ~/bin/

cd ~/projects/myrepo-sync/
rsync --delete --exclude-from=.gitignore --max-size=1m -r ~/projects/myrepo/ ~/projects/myrepo-sync/ 
git add -A
git commit -a -m auto
git push origin myrepo-sync

The really cool thing about this method are: 1. You get "free" continuous history running in the background. 2. It's the same place as your git repository, so as long as that is backed up, so will you history. 3. You have a branch with all your edits without having to manually commit them. If you wanted to diff the current file with contents from 2 hours ago all that information is in the git repo. 4. Don't have to rely on an IDE for file history. You can make changes in multiple editors.

Sharp Edges

  1. If you create a large file in the directory of your git repository. The backup will pick it up unless the directory is in .gitignore or the size is greater than 1m. Removing files and their references in git can be a pain.
  2. Don't forget to turn this on for every git repo you want to backup.
  3. Don't forget this is pu``shed to a git repository that other people potentially have access to.