-
Star
(428)
You must be signed in to star a gist -
Fork
(79)
You must be signed in to fork a gist
-
-
Save karpathy/1dd0294ef9567971c1e4348a90d69285 to your computer and use it in GitHub Desktop.
| # ----------------------------------------------------------------------------- | |
| # AI-powered Git Commit Function | |
| # Copy paste this gist into your ~/.bashrc or ~/.zshrc to gain the `gcm` command. It: | |
| # 1) gets the current staged changed diff | |
| # 2) sends them to an LLM to write the git commit message | |
| # 3) allows you to easily accept, edit, regenerate, cancel | |
| # But - just read and edit the code however you like | |
| # the `llm` CLI util is awesome, can get it here: https://llm.datasette.io/en/stable/ | |
| gcm() { | |
| # Function to generate commit message | |
| generate_commit_message() { | |
| git diff --cached | llm " | |
| Below is a diff of all staged changes, coming from the command: | |
| \`\`\` | |
| git diff --cached | |
| \`\`\` | |
| Please generate a concise, one-line commit message for these changes." | |
| } | |
| # Function to read user input compatibly with both Bash and Zsh | |
| read_input() { | |
| if [ -n "$ZSH_VERSION" ]; then | |
| echo -n "$1" | |
| read -r REPLY | |
| else | |
| read -p "$1" -r REPLY | |
| fi | |
| } | |
| # Main script | |
| echo "Generating AI-powered commit message..." | |
| commit_message=$(generate_commit_message) | |
| while true; do | |
| echo -e "\nProposed commit message:" | |
| echo "$commit_message" | |
| read_input "Do you want to (a)ccept, (e)dit, (r)egenerate, or (c)ancel? " | |
| choice=$REPLY | |
| case "$choice" in | |
| a|A ) | |
| if git commit -m "$commit_message"; then | |
| echo "Changes committed successfully!" | |
| return 0 | |
| else | |
| echo "Commit failed. Please check your changes and try again." | |
| return 1 | |
| fi | |
| ;; | |
| e|E ) | |
| read_input "Enter your commit message: " | |
| commit_message=$REPLY | |
| if [ -n "$commit_message" ] && git commit -m "$commit_message"; then | |
| echo "Changes committed successfully with your message!" | |
| return 0 | |
| else | |
| echo "Commit failed. Please check your message and try again." | |
| return 1 | |
| fi | |
| ;; | |
| r|R ) | |
| echo "Regenerating commit message..." | |
| commit_message=$(generate_commit_message) | |
| ;; | |
| c|C ) | |
| echo "Commit cancelled." | |
| return 1 | |
| ;; | |
| * ) | |
| echo "Invalid choice. Please try again." | |
| ;; | |
| esac | |
| done | |
| } |
Here's a more convenient git ai copilot: https://github.com/Undertone0809/gcop
Benefits:
- Easier to use with git alias, which means just use the git command
- Compatible with all LLMs, easy to configure no matter what model you're using.
- Various git alias extensions to help you use git commands more easily.
If you're using a different model other then gpt-4o-mini, you can change the default model that llm uses with
llm models default <insert model>
I wanted to try this out with a local model so I followed the guide for setting up llama3.2 on llm:
# Install the plugin
llm install llm-ollama
# Download and run a prompt against the Orca Mini 7B model
ollama pull llama3.2:latest
llm -m llama3.2:latest 'What is the capital of France?'
and then set llama3.2 as my default:
llm models default llama3.2:latest
guys, use https://github.com/di-sukharev/opencommit, it's the most feature rich open-source tool
run
npm i -g opencommit, thenoco config set OCO_OPENAI_API_KEY=your_keyand then sumply runocoto generate your git commit messages.πππ
Awesome!!! πππ
In case anyone runs into the same error as me, you might need to unalias gcm before the gist above:
# Unalias gcm to allow for custom function unalias gcm 2>/dev/nullFor me this was set to
git checkout mainpreviously by the Oh My Zsh plugin
Oh, nice job! Thx! π
i suggest adding a parameter to bypass the ai-generated message when the user passes the
-mflagso that
gcm -m 'my user-generated commit msg'allows a user to enter their own message without pinging the llm.to do that you can add the following to line 11.