GPTCommit is an automated Git commit tool. It leverages OpenAI's GPT-4o model to analyze code changes and generate commit messages, simplifying the code submission process.
- Automatically detects uncommitted changes
- Generates commit messages using the GPT-4o model
- Automatically executes
git add .
andgit commit -m {message}
-
Clone this repository to your local machine:
git clone https://github.com/zhufengme/GPTCommit
-
Navigate to the project directory:
cd GPTCommit
-
Ensure you have
jq
installed to handle JSON data. If not, you can install it using the following command:sudo apt-get install jq
Before running the script, you need to configure a few things.
-
Open the
gptcommit.sh
file:nano gptcommit.sh
-
Replace
your_openai_api_key_here
in the following line with your OpenAI API key:OPENAI_API_KEY="your_openai_api_key_here"
-
[Optional] Set the proxy you want to use. Leave it empty to follow curl's default behavior (using the HTTPS_PROXY environment variable or settings in .curlrc):
CURL_PROXY=""
-
[Optional] If you need to modify the OpenAI API endpoint, you can edit the following line:
OPENAI_API_ENDPOINT="https://api.openai.com/v1/chat/completions"
You can change it to a different API endpoint as needed.
-
[Optional] The default commit message is generated in English. You can edit the following line to change the default language:
LANGUAGES="en"
You can change it to a different language, such as
zh
for Chinese.
-
Ensure there are uncommitted changes in your Git working directory.
-
Run the script:
./gptcommit.sh
-
The script will automatically perform the following steps:
- Check the working directory status
- Retrieve uncommitted changes
- Call OpenAI's API to generate a commit message
- Add changes to the staging area and commit
The script supports two optional command line parameters:
-
--lang
: Temporarily specify the language(s) of the generated commit message. For example:./gptcommit.sh --lang=zh
This command generates the commit message in Chinese.
./gptcommit.sh --lang=zh,en
This command generates the commit message in both Chinese and English.
-
--notes
: Add extra notes or instructions for the commit message generation. For example:./gptcommit.sh --notes="Focus on performance improvements"
This command will instruct the AI to pay special attention to performance-related changes when generating the commit message.
You can use these parameters together:
./gptcommit.sh --lang=zh,en --notes="Highlight security updates"
This command will generate a commit message in both Chinese and English, with a focus on security-related changes.
To conveniently use the script from any directory, you can add it to the system PATH:
-
Copy the script to a directory in your PATH, such as
/usr/local/bin
:sudo cp gptcommit.sh /usr/local/bin/gptcommit sudo chmod +x /usr/local/bin/gptcommit
-
Now you can use the script in any Git repository directory by running:
gptcommit
- Ensure your OpenAI API key has sufficient quota to handle requests.
- The script assumes you are working within a current Git repository and have appropriate permissions to perform commit operations.
- Ensure your network connection is stable, especially if you need to access the OpenAI API via a proxy.
Issues and feature requests are welcome! If you want to contribute code, please fork this repository and submit a pull request.
This project is licensed under the GPL.