Skip to content

Instantly share code, notes, and snippets.

@karpathy
Created August 25, 2024 20:43
Show Gist options
  • Save karpathy/1dd0294ef9567971c1e4348a90d69285 to your computer and use it in GitHub Desktop.
Save karpathy/1dd0294ef9567971c1e4348a90d69285 to your computer and use it in GitHub Desktop.
Git Commit Message AI
# -----------------------------------------------------------------------------
# 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
}
@davidoort
Copy link

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/null

For me this was set to git checkout main previously by the Oh My Zsh plugin

@knyazer
Copy link

knyazer commented Aug 26, 2024

Fork for fish with basic support for git flags: https://gist.github.com/knyazer/675e6eb945ae5ec64af2f9be4826b07e

@voodoohop
Copy link

I've been using this one for a while. it has a little more detail on how a commit message should be formatted.

git commit . -m "$(git diff | sgpt --model gpt-4o --code 'Write concise, informative commit messages: Start with a summary in imperative mood, explain the 'why' behind changes, keep the summary under 50 characters, use bullet points for multiple changes, avoid using the word refactor, instead explain what was done, and reference related issues or tickets. What you write will be passed to git commit -m "[message]"')"

@Suenym
Copy link

Suenym commented Aug 26, 2024

what about small models with 7b parametres, is it will be good at it?

@gmaijoe
Copy link

gmaijoe commented Aug 26, 2024

you can also accomplish this using code ~/.gitconfig

and adding:

[alias]
    ai = "!f() { git add . && if [ -n \"$(git diff --cached)\" ]; then git commit -m \"$(git diff --cached | llm -m '4o-mini' 'Below is a diff of all staged changes, coming from the command:\\n```\\ngit diff --cached\\n```\\nPlease generate a concise, one-line commit message for these changes.')\" && git push; else echo 'No changes to commit'; fi }; f"

for some reason the zshrc option lost my local branch context but .gitconfig worked

Additionally, you can also set up a vscode keybinding (cmd + shift + P on mac) to open up the JSON:

// Place your key bindings in this file to override the defaultsauto[]
[
    {
        "key": "cmd+enter",
        "command": "workbench.action.terminal.sendSequence",
        "args": { "text": "git ai\n" }
    },
....

now you can just do "cmd + enter" to get commit messages quickly.

It goes without saying that you should install [llm](https://llm.datasette.io/en/stable/) locally via brew or another method and connect your openai api key.

@rorybyrne
Copy link

I have been using:

git commit -m "$(git diff --staged | sgpt "Write a single conventional commits style commit message for this diff on the branch $(git rev-parse --abbrev-ref HEAD).")"

@gel
Copy link

gel commented Aug 26, 2024

Extremely useful!

(1) I have replaced llm with ollama run llama3.1.
(2) Something was already taking the gcm command, I added unalias gcm 2>/dev/null before.

@Fl0p
Copy link

Fl0p commented Aug 26, 2024

check this https://github.com/Fl0p/gitpmoji
Screenshot 2024-08-26 at 16 46 45
generate commit message / decorate with emoji / give rating / evaluate diff
pros - uses git hook don't need any additional. cons - uses only openAI api

@mondalsandy02
Copy link

So cool

@austinemuangstubbs
Copy link

For oh-my-zsh users, on .zshrc I added

# must unalias all ZSH defaults here AFTER we source the above
unalias gcm 

after

source $ZSH/oh-my-zsh.sh

To make sure the gcm alias stays ripped out between sessions.

@HtutLynn
Copy link

Forked this no-brainer cheap solution for Git commits!

Google Gemini works great with it, especially with the free tier. For testing, use gemini-1.5-flash. Here is my fork,

https://gist.github.com/HtutLynn/7fe83d64d6381726ac06e9fb7f972643

Quick start:

Install the Gemini plugin:

llm install llm-gemini

Set up your API key.

Happy committing! πŸš€

@koisose
Copy link

koisose commented Aug 27, 2024

@karpathy i make the nodejs version https://github.com/koisose/auto-commit-gaia no need for api key coz its using gaianet.ai also using llama

@koisose
Copy link

koisose commented Aug 27, 2024

Forked this no-brainer cheap solution for Git commits!

Google Gemini works great with it, especially with the free tier. For testing, use gemini-1.5-flash. Here is my fork,

https://gist.github.com/HtutLynn/7fe83d64d6381726ac06e9fb7f972643

Quick start:

Install the Gemini plugin:

llm install llm-gemini

Set up your API key.

Happy committing! πŸš€

@HtutLynn cant agree more i create both for gemini and groq as well the groq one using gemma and its super fast as well https://github.com/koisose/auto-commit-gemini and https://github.com/koisose/auto-commit-groq

@di-sukharev
Copy link

di-sukharev commented Aug 27, 2024

guys, use https://github.com/di-sukharev/opencommit, it's the most feature rich open-source tool

run npm i -g opencommit, then oco config set OCO_OPENAI_API_KEY=your_key and then sumply run oco to generate your git commit messages.

πŸš€πŸš€πŸš€

@kybik44
Copy link

kybik44 commented Aug 27, 2024

guys, use https://github.com/di-sukharev/opencommit, it's the most feature reach open-source tool

run npm i -g opencommit, then oco config set OCO_OPENAI_API_KEY=your_key and then sumply run oco to generate your git commit messages.

πŸš€πŸš€πŸš€

Oh, nice job! ThxπŸ‘‘

@cocaKolya
Copy link

guys, use https://github.com/di-sukharev/opencommit, it's the most feature reach open-source tool

run npm i -g opencommit, then oco config set OCO_OPENAI_API_KEY=your_key and then sumply run oco to generate your git commit messages.

πŸš€πŸš€πŸš€

πŸ‘πŸ‘

@wakeoneself
Copy link

guys, use https://github.com/di-sukharev/opencommit, it's the most feature reach open-source tool

run npm i -g opencommit, then oco config set OCO_OPENAI_API_KEY=your_key and then sumply run oco to generate your git commit messages.

πŸš€πŸš€πŸš€

Oh, I was looking for this repo but forgot the name, thanks!

@di-sukharev
Copy link

guys, use https://github.com/di-sukharev/opencommit, it's the most feature reach open-source tool
run npm i -g opencommit, then oco config set OCO_OPENAI_API_KEY=your_key and then sumply run oco to generate your git commit messages.
πŸš€πŸš€πŸš€

Oh, I was looking for this repo but forgot the name, thanks!

You are welcome 🎩

@di-sukharev
Copy link

check this https://github.com/Fl0p/gitpmoji Screenshot 2024-08-26 at 16 46 45 generate commit message / decorate with emoji / give rating / evaluate diff pros - uses git hook don't need any additional. cons - uses only openAI api

take a look at https://github.com/di-sukharev/opencommit, we even support llama running locally or remotely

@di-sukharev
Copy link

I have been using:

git commit -m "$(git diff --staged | sgpt "Write a single conventional commits style commit message for this diff on the branch $(git rev-parse --abbrev-ref HEAD).")"

πŸ‘†

@vlameiras
Copy link

Made some changes so it doesn't make calls when there are no staged changes. Renamed the command to gai due to conflict with the Oh My ZSH alias

https://gist.github.com/vlameiras/8591bf75990571f5f1e2ea9c452047c3

@ammario
Copy link

ammario commented Aug 29, 2024

Similar tool at coder/aicommit that follows a repo's existing commit style.

@appleseed-iii
Copy link

i suggest adding a parameter to bypass the ai-generated message when the user passes the -m flag

so 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.

 # Check if -m flag is provided
if [ "$1" = "-m" ] && [ -n "$2" ]; then
    # Use the provided message directly
    if git commit -m "$2"; then
        echo "Changes committed successfully with your message!"
        return 0
    else
        echo "Commit failed. Please check your message and try again."
        return 1
    fi
fi

@Undertone0809
Copy link

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.

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