DEV Community

Cover image for Keeping Git Commit Messages Consistent with a Custom Template

Keeping Git Commit Messages Consistent with a Custom Template

Timothy Merritt on January 10, 2021

xkcd on git commit messages The ability to write consistent git commit messages is a skill that will serve both you and any team you’re working...
Collapse
 
haxnet profile image
HaxNet

Thanks for letting me know about this. I use git here and there and like the feature of template.

git config --global core.editor=nvim +14 -c 'startinsert' didn't work for me

I use arch, not sure if that's the reason, but for anyone having trouble with this.
My fix

git config --global core.editor 'vim +14 +startinsert'

cheers

Collapse
 
timmybytes profile image
Timothy Merritt

Thanks for pointing this out, @haxnet . I've edited the article to include Neovim and Vim configurations, with links to more info!

Collapse
 
joelrozen profile image
Joel Rozen

Conventional Commits node package also does this for you but gives you a nice interactive cli asking you to choose / enter descriptions etc.

Collapse
 
timmybytes profile image
Timothy Merritt

Thanks for this! It seems really slick. For anyone else interested, the project is called commitlint.

Collapse
 
michaelcurrin profile image
Michael Currin

Can I suggest updating your example to match the style that is actually recommended on Conventional Commits site which you linked to.

-(docs): Update README with contributing instructions
+docs: Update README with contributing instructions
Enter fullscreen mode Exit fullscreen mode

Remove the brackets. Brackets are for scope.

Based on the link, if you were working on adding a language to the docs, you could add scope.

docs(lang): Add Polish section
Enter fullscreen mode Exit fullscreen mode

I think the scope is also useful for module. For example if you have foo and bar in src directory, you would do feat(foo) to show the commits are scoped to the foo module.

The scope can be whatever you want but the advice is your team needs to agree on it and stick to it.

Collapse
 
timmybytes profile image
Timothy Merritt

Thanks for mentioning this. When I originally started using the convention, I didn't use the scope/module designator, since for many smaller projects it can be overkill. I just kept the parentheses around the type for aesthetics, but I see how that could be confusing for someone adopting the conventional method (and/or make the template incompatible with other conventional commit tools). I've updated the article to include these changes, and also added more info on structuring a commit message in my git configuration.

Cheers!

Collapse
 
michaelcurrin profile image
Michael Currin

That linked page looks good.

That's a lot of dotfiles. Here are just a few of mine github.com/MichaelCurrin/dotfiles

Collapse
 
michaelcurrin profile image
Michael Currin

Happy to help :)

Also you could turn your help message into a shell alias or a bookmarked page / gist so you can read it anytime without committing.

Collapse
 
michaelcurrin profile image
Michael Currin

Something useful to add to your available type is chore. Such as deleting or renaming a file. Or changing a config like .gitignore which relates more to the repo itself than your application's build flow.

I started using build for only build-related configs - like a package.json or lint config file.

Collapse
 
timmybytes profile image
Timothy Merritt

I actually had already been using chore, but forgot to add it here! The article's been updated to include it. Thanks for mentioning it!

Collapse
 
michaelcurrin profile image
Michael Currin

You're welcome.

I'd appreciate if you have a look at my extension which doesn't let you choose a semantic message, it writes one for you.

github.com/MichaelCurrin/auto-comm...

Collapse
 
hatemhosny profile image
Hatem Hosny

There is also a nice vscode extension for conventional commits

Collapse
 
pepellou profile image
β™œ Pepe Doval

Nice, thanks for sharing!

Just a minor typo: the git-config to set line 14 on vim should say core.editor instead of core-editor.

Collapse
 
timmybytes profile image
Timothy Merritt

Thanks for catching that! It’s been edited and updated. Cheers!

Collapse
 
phongduong profile image
Phong Duong

Thank you for sharing

Collapse
 
lavinj87 profile image
Jorge Lavin

Thanks, really useful.

You may want to add that the comment character is configurable. In my case is ; so I modified the template accordingly.

Collapse
 
timmybytes profile image
Timothy Merritt

Thanks! I've edited to include info on configuring the comment character. From git's documentation, both the # and ; characters are valid by default, but in case anyone wants theirs to be "🀫" or something, there is info on how to set it now!

Collapse
 
petroskoulianos profile image
Petros Koulianos

This is great 😎. I was thinking if i was able to do such a think. It is very useful for dev teams working with git - issues - branches.
Thanks for sharing Timothy.

Collapse
 
murkrage profile image
Mike Ekkel

I like the idea of creating a commit message template :). Might end up proposing this to the teams at my company!