Skip to content

Instantly share code, notes, and snippets.

@azu
Created July 7, 2023 04:49
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 azu/5b1def49b0737daab23d0320ce3ce67e to your computer and use it in GitHub Desktop.
Save azu/5b1def49b0737daab23d0320ce3ce67e to your computer and use it in GitHub Desktop.
git auto commit
function git-auto-commit(){
git_status=$(git status --porcelain | grep -vE '^\s')
first=$(echo "$git_status" | head -n1)
filename=$(echo "$first" | awk '{print $NF}')
git_root_dir=$(git rev-parse --show-toplevel)
if echo "$first" | grep -Eq '^\s?M'; then
commit_message="Update $filename"
elif echo "$first" | grep -Eq '^\s?A'; then
commit_message="Add $filename"
elif echo "$first" | grep -Eq '^\s?D'; then
commit_message="Delete $filename"
elif echo "$first" | grep -Eq '^\s?R'; then
from=$(echo "$first" | awk '{print $2}')
to=$(echo "$first" | awk '{print $4}')
commit_message="Rename $from to $to"
else
echo "🔔 untrack file exists"
echo "$(git status --porcelain)"
# confirm to add all untrack files(Y/n/Enter)
echo "🤖 add all untrack files? (y/n)"
read answer
if [ "$answer" != "y" ]; then
return 1
fi
git add ${git_root_dir} --verbose
git-auto-commit
return 0
fi
git commit -m "$commit_message"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment