Last active
December 12, 2025 16:19
-
-
Save atifaziz/b6b3d9e3de3c78c17eb743497f1806e8 to your computer and use it in GitHub Desktop.
Bash script to ask Copilot to suggest Git commit messages based on staged changes
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
| #!/usr/bin/env bash | |
| set -eo pipefail | |
| prompt=$(cat <<'EOF' | |
| Suggest 3 one-liner & brief Git commit messages, each formatted EXACTLY as: | |
| MESSAGE:Text of the commit message | |
| Examples: | |
| MESSAGE:First suggestion message | |
| MESSAGE:Second suggestion message | |
| MESSAGE:Third suggestion message | |
| Only suggest commit messages that reflect staged changes. If there are no staged | |
| changes then ask user to stage changes and do not suggest any commit messages. | |
| EOF | |
| ) | |
| mapfile suggestions < <( | |
| copilot -p "$prompt" | | |
| tee >(grep -F MESSAGE: | sed 's/MESSAGE: *//') \ | |
| >(grep -vF MESSAGE: >&2) \ | |
| >/dev/null | |
| ) | |
| if [ "${#suggestions[@]}" -ne 3 ]; then | |
| echo "Expected 3 suggestions, got ${#suggestions[@]}" >&2 | |
| exit 1 | |
| fi | |
| n=1 | |
| for suggestion in "${suggestions[@]}"; do | |
| echo -n "$n) $suggestion" | |
| ((n++)) | |
| done | |
| printf 'q) Quit without committing | |
| Select your preference (1/2/3/q): ' | |
| read selection | |
| if [[ ! "$selection" =~ ^[1-3q]$ ]]; then | |
| echo "Invalid selection: $selection" >&2 | |
| exit 1 | |
| fi | |
| if [[ "$selection" == "q" ]]; then | |
| exit 1 | |
| fi | |
| selected=${suggestions[$(($selection - 1))]} | |
| git commit -eam "$selected" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment