DEV Community

Cover image for git-auto-commit: Streamline Your Git Development Workflow with ShellGPT
Choz
Choz

Posted on

git-auto-commit: Streamline Your Git Development Workflow with ShellGPT

git-auto-commit demo

Why Automate Git Commit Messages?

Automated commit messages can save time, reduce errors, and ensure consistency across a project's history. When done right, they can provide a clear, concise log of changes that are invaluable for team collaboration and project maintenance, is what GPT says. For me, I am just too lazy and it seems to do the job, so...

Prerequisites

Setting the Stage for Automation

  • Install python3 and python3-pip
  • Obtain OpenAI API Key - You can grab your key from this OpenAI page
  • Setting up ShellGPT - You can view their github repo and there is a good article to set it up here by Beebom

Configuration Steps

Integrating ShellGPT with Your Linux Environment

Introduce the git-auto-commit function.
Paste the following code into your .bashrc file.

#.bashrc

# ShellGPT
export OPENAI_API_KEY=<YOUR_OPENAI_API_KEY>
function git-auto-commit() {                                                                                                                        
  # Check for staged changes
  if ! git diff --cached --exit-code --quiet; then
    # Allow optional prepended text to the commit message
    local prepend_text="$1"

    # Check the length of the diff
    local diff_length=$(git --no-pager diff --staged | wc -l)
    local max_length=500 # Example threshold, adjust as needed

    if [ $diff_length -gt $max_length ]; then
      echo "The staged diff is too long ($diff_length lines). Please consider committing fewer changes at a time for easier review."
      return 1
    fi

    # Generate the commit message with optional prepending
    local message=$(git --no-pager diff --staged | sgpt --model gpt-3.5-turbo-16k-0613 'Based on the provided git diff, generate a concise and informative commit message that clearly explains the changes to other developers. Aim for brevity but ensure the message captures the essence of the modifications and their impact on the project: ')
    if [[ -n "$prepend_text" ]]; then
      message="$prepend_text $message"
    fi

    # Attempt to commit and handle possible failure
    if ! git commit -m "$message"; then
      echo "Commit failed. Please check your staged changes or commit message."
      return 1
    fi
  else
    echo "No staged changes to commit."
    return 0
  fi
}
alias gac='git-auto-commit'
Enter fullscreen mode Exit fullscreen mode

Usage

Add your changes to the staging area.

git add yourfile1.ts yourfile2.ts
Enter fullscreen mode Exit fullscreen mode

Trigger the git-auto-commit or gac for its alias;

gac
Enter fullscreen mode Exit fullscreen mode

You can also prepend the commit message if you need to tag something in your workflow

gac "OK-12345 Bugfix: "
Enter fullscreen mode Exit fullscreen mode

And, that's it! Now you dont need to worry to write your commit message everytime.

Notice that we're using gpt-3.5-turbo-16k-0613 model, as I observed that it's sufficient to do the job and 10x more affordable than any of gpt-4 models.

Next to consider

Challenges

The function includes a preemptive check for the size of the staged changes. If the changes exceed 500 lines (a customizable threshold), it advises reducing the size of the commit. This encourages developers to make smaller, more manageable commits, which are easier to review and understand.

Options to Tweak the Prompt

Customizing the prompt sent to ShellGPT allows for more tailored commit messages. This can be modified in the .bashrc file that you saw above.

Adjusting Verbosity

Include flags or parameters in your function to adjust the level of detail in the generated commit messages, catering to different project needs.

Credits

Conclusion

Hope you find it useful. Good luck, have fun!

Top comments (0)