Skip to content

Instantly share code, notes, and snippets.

@litanlitudan
Last active December 23, 2021 06:46
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 litanlitudan/935cc3bb4bd79101f44ce7551004cfcb to your computer and use it in GitHub Desktop.
Save litanlitudan/935cc3bb4bd79101f44ce7551004cfcb to your computer and use it in GitHub Desktop.
Automatic git commit

Overview

This gist shows the steps to automatically git commit changes under a folder of interest to your github.

Step 0

Create a repo on github for the folder of interest and intialize the folder as a git repo by git init

Step 1

Create a subfolder (say scripts) and download the aut-commit.sh to that folder.

cd /path/to/the/folder/of/interest
mkdir scripts # create a subfolder
cd scripts && wget https://gist.githubusercontent.com/litanlitudan/935cc3bb4bd79101f44ce7551004cfcb/raw/901631a309ad0cb2b7d25aa2aac77710a6143f3a/auto-commit.sh # download aut-commit.sh to the folder

Sanity check: The folder's structure should look like this

folder_of_interest
├── .git
├── file_of_interest.cpp
├── file_of_interest.md
├── file_of_interest.py
├── file_of_interest.txt
└── scripts
    └── auto-commit.sh

Step 2

Add the following line to your crontab

*/5 * * * * /path/to/the/folder/of/interest/scripts/auto-commit.sh >/dev/null 1>&1

To open the crontab in macOS, use crontab -e in the terminal

Caveat: /path/to/the/folder/of/interest needs to be absolute path which starts with /

Now all changes under /path/to/the/folder/of/interest will be autoamtically committed and pushed to github every 5 minutes.

#!/usr/bin/env sh
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
cd "$SCRIPT_DIR/../"
git pull
CHANGES_EXIST="$(git status --porcelain | wc -l)"
if [ "$CHANGES_EXIST" -eq 0 ]; then
exit 0
fi
git add .
git commit -q -m "[MBP] Auto Commit: $(date +"%Y-%m-%d %H:%M:%S")"
git push -q
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment