Git auto-commit & push every couple of minutes

Jul 13, 2020 21:30 · 158 words · 1 minute read

1
watch -n <SECONDS-INTERVAL> "git pull && (git ls-files --modified --others --exclude-standard | grep . > /dev/null) && { git add . ; git commit -m '<MESSAGE>' ; git push; }"

What does it exactly do?

  • watch executes a command periodically
  • git ls-files lists all modified files, and all “other” (e.g. untracked) files. Additionally, we make sure the usual .gitignore rules apply via the flag --exclude-standard. The important part is to then to do a wildcard grep search on the result ("." stands for “any character is fine”). But we are not interested in the actual result, and therefore throw it away by redirecting the output to /dev/null. We are interested in the exit state: grep exits with an error code in case no result is found. Thus, only if there are uncommitted changes, the program continues, where we actually add, commit, and push the changes.

Check out the exact explanations for the git parts with ExplainShell 😁