Last active
January 7, 2026 06:58
-
-
Save sajaddp/98908cc2f958a47f1575cc03ec52f88c to your computer and use it in GitHub Desktop.
Auto commit each changed file with generated message
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # ------------------------------------------------------------ | |
| # auto-commit | |
| # Commit each changed file separately with an auto-generated | |
| # commit message based on the file status. | |
| # | |
| # Features: | |
| # - Creates one commit per file | |
| # - Generates message: Create / Modify / Delete / Rename / Copy | |
| # - Refuses to run if staged changes already exist | |
| # | |
| # Requirements: | |
| # - Must be inside a git repository | |
| # - Working tree may have changes, but index must be clean | |
| # | |
| # Install: | |
| # curl -fsSL "https://gist.githubusercontent.com/sajaddp/98908cc2f958a47f1575cc03ec52f88c/raw/7c8794e1ffde7234881c86608cc22f94842405cd/auto-commit.sh" -o ~/.auto-commit.sh | |
| # echo 'source ~/.auto-commit.sh' >> ~/.bashrc | |
| # source ~/.bashrc | |
| # | |
| # Usage: | |
| # ac | |
| # | |
| # Notes: | |
| # - The function name is auto_commit | |
| # - The command alias provided is: ac | |
| # ------------------------------------------------------------ | |
| auto_commit() { | |
| git rev-parse --is-inside-work-tree >/dev/null 2>&1 || return 1 | |
| git diff --cached --quiet -- || { | |
| echo "Staged changes exist. Commit/reset them first." >&2 | |
| return 2 | |
| } | |
| local entry st p1 p2 msg | |
| while IFS= read -r -d '' entry; do | |
| st="${entry:0:2}" | |
| p1="${entry:3}" | |
| p2="" | |
| if [[ "$st" == *R* || "$st" == *C* ]]; then | |
| IFS= read -r -d '' p2 || p2="" | |
| fi | |
| [[ -n "$p1" ]] || continue | |
| case "$st" in | |
| '??') msg="Create $p1" ;; | |
| *A*) msg="Create $p1" ;; | |
| *M*) msg="Modify $p1" ;; | |
| *D*) msg="Delete $p1" ;; | |
| *R*) msg="Rename $p1 -> $p2" ;; | |
| *C*) msg="Copy $p1 -> $p2" ;; | |
| *) msg="Update $p1" ;; | |
| esac | |
| msg="${msg//$'\n'/ }" | |
| if [[ "$st" == *R* || "$st" == *C* ]]; then | |
| [[ -n "$p2" ]] || { echo "Bad rename/copy entry for: $p1" >&2; return 1; } | |
| git add -A -- "$p1" "$p2" || return 1 | |
| else | |
| git add -A -- "$p1" || return 1 | |
| fi | |
| git diff --cached --quiet -- && continue | |
| git commit -m "$msg" || return 1 | |
| done < <(git status --porcelain=v1 -z --untracked-files=all) | |
| } | |
| alias ac="auto_commit" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment