Skip to content

Instantly share code, notes, and snippets.

@defclass
Last active February 16, 2024 02:45
Show Gist options
  • Save defclass/848eedef3716f6937e1a5f476155f6cd to your computer and use it in GitHub Desktop.
Save defclass/848eedef3716f6937e1a5f476155f6cd to your computer and use it in GitHub Desktop.
auto push to Github when files change
#!/bin/bash
## Tested on macOS big sur 11.2.1
## Usage:
## git-auto ;; use current script dir as git dir, and auto commit, not push.
## git-auto -d /path/to/your/note's/dir ;; set git dir
## git-auto -p ;; auto commit and push
## git-auto -s origin -p ;; set remote server
## git-auto -b main -p ;; set git branch
## git-auto -i 30 -p ;; set interval seconds
## git-auto -o -p;; execute once
#set -e
#set -x
usage="usage: $0 [-d <git directory>] [-i <interval seconds>] [-p <push to remote server>] [-s git remote server] [-b git branch] [-o <execute once]"
push_to_server=0
server=origin
interval=20
once=0
branch=$(git rev-parse --abbrev-ref HEAD)
OPTIND=1
while getopts d:i:b:s:po flag; do
case "${flag}" in
d) directory=${OPTARG} ;;
p) push_to_server=1 ;;
o) once=1 ;;
s) server=${OPTARG} ;;
b) branch=${OPTARG} ;;
i) interval=${OPTARG} ;;
*)
echo "ERROR: ${usage}" >&2
exit 1
;;
esac
done
shift $((OPTIND - 1))
if [[ "${directory}" ]]; then
cd "${directory}" || exit 1
fi
function auto-commit-and-push() {
if ! [[ $(git status) =~ "working tree clean" ]]; then
git add .
git commit -m "auto commit"
if [[ 1 == "${push_to_server}" ]]; then
git push "${server}" "${branch}"
fi
fi
}
date
if [[ 1 == "${once}" ]]; then
auto-commit-and-push
else
while true; do
auto-commit-and-push
sleep "${interval}"
done
fi
@defclass
Copy link
Author

created a repo to receive PRs https://github.com/defclass/git-auto

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