Skip to content

Instantly share code, notes, and snippets.

@AhmedElwerdany
Created February 3, 2026 10:38
Show Gist options
  • Select an option

  • Save AhmedElwerdany/18f86d28c373e926f997b2a2c693306a to your computer and use it in GitHub Desktop.

Select an option

Save AhmedElwerdany/18f86d28c373e926f997b2a2c693306a to your computer and use it in GitHub Desktop.
Generate Git commit messages via ChatGPT (staged diff)
#!/usr/bin/env bash
set -euo pipefail
diff="$(git diff --cached)"
if [[ -z "$diff" ]]; then
echo "No staged changes."
exit 1
fi
prompt=$(printf "Write a clear, professional Git commit message summarizing these changes.
\n%s" "$diff")
# Copy to clipboard
if command -v wl-copy >/dev/null; then
printf "%s" "$prompt" | wl-copy
elif command -v xclip >/dev/null; then
printf "%s" "$prompt" | xclip -selection clipboard
elif command -v pbcopy >/dev/null; then
printf "%s" "$prompt" | pbcopy
fi
# URL encode CORRECTLY
encoded=$(printf "%s" "$prompt" | python3 -c 'import sys,urllib.parse; print(urllib.parse.quote(sys.stdin.read()))')
# Open with prompt
xdg-open "https://chatgpt.com/?prompt=$encoded"
@AhmedElwerdany
Copy link
Author

Installation (Global Command)

Option 1 — Install to /usr/local/bin (recommended)

This makes gitcommitgpt available system-wide.

# Download or save the script as: gitcommitgpt
sudo mv gitcommitgpt /usr/local/bin/gitcommitgpt
sudo chmod +x /usr/local/bin/gitcommitgpt

Verify:

which gitcommitgpt
gitcommitgpt

Option 2 — Install to ~/.local/bin (no sudo)

For user-only installation:

mkdir -p ~/.local/bin
mv gitcommitgpt ~/.local/bin/gitcommitgpt
chmod +x ~/.local/bin/gitcommitgpt

Ensure it’s in your PATH:

echo $PATH | grep "$HOME/.local/bin" || echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.bashrc

Reload shell:

source ~/.bashrc

Requirements

  • git

  • One of:

    • xclip (X11)
    • wl-copy (Wayland)
    • pbcopy (macOS)
  • python3


Usage

gitcommitgpt

Stages must already be added with:

git add .

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