How to setup Copilot to autocomplete commit messages

Close up of the center console of an airplane with two pilots sitting each side.
Photo by Franz Harvin Aceituna

Copilot, GitHub's AI programming assistant, has been a great addition to my development environment by giving (mostly) intelligent suggestions and autocompletion. Somewhat accidentally I found that Copilot was also good at writing commit messages. In this post, I will show how you can use Copilot to autocomplete your Git commit messages.


On writing good commits

Writing clear and accurate commit messages is super helpful for future developers (including yourself) to understand why changes were made. It's great for debugging and getting a grasp on the code you're working with.

Luckily, Copilot can assist with this and works best when the content of a commit is specific and related. And that's exactly what we want! There are plenty of resources available on writing commit messages, so I won't dive into that more here. Feel free to check out these links for some excellent content on the topic:


Setting up your commit environment

Setting up a commit message template

For a long time, I have used the a git commit message template to improve the quality of my commit messages. The template consists of a sentence starter which helps me write a commit message in an imperative voice.

To setup a commit message template add a file in your home directory called .git-commit-message.txt. Inside setup your template comments. Mine looks like this:

# When applied, this commit will (in less than 72 characters)...

# Why is this change needed?

# How does it address the issue?

# Provide links to any relevant tickets, articles or other resources
Including the character limit ensures Copilot doesn’t get too verbose with the message.

To configure git to use this as your commit message template run:

git config --global commit.template ~/.git-commit-template.txt

Once setup, Copilot can use this prompt and try to form a commit message. But without knowing what’s changed it probably won’t be very successful. This is where verbose mode comes in.

Git commit verbose mode

Verbose mode shows a unified diff of what would be committed at the bottom of the commit message template.

To use verbose mode we can use the --verbose flag. E.g. git commit --verbose. We can also enable verbose mode by default by running:

git config --global commit.verbose true

Now that the commit diff is in the commit message template, Copilot can use this as context for generating the commit message.

Commit message keyword

Finally, Copilot needs at least one word to begin suggesting a commit message. You can check out the Git Commit Message guide for a list of suggested keywords to get you started.

A few example keywords are Add, Drop, Fix, Make, Refactor, Reformat, among others. Once you've staged some changes using git add, run git commit and start writing the commit message using one of these keywords. Copilot will then begin suggesting the rest of the commit message. 🚀


Copilot in action

Screenshot of editing a commit message that includes a change to the theme color of my website. Copilot completes the commit message with 'Update theme color to match background'.
Example of copilot completion in neovim. 😙👌

Obviously, Copilot isn't always spot-on. It can make mistakes and give wrong suggestions at times. So, it's important to take its input with a grain of salt and trust your own judgment. Good commit messages should be informative and accurate, so don't forget to review and confirm the commit messages before finalising them.

Apart from that, have fun!


Meta

Comments