DEV Community

Cover image for Git Good: Best Practices for Branch Naming and Commit Messages

Git Good: Best Practices for Branch Naming and Commit Messages

shinjith on February 03, 2024

Hello devs, I'm going to share a few best practices for using Git more effectively. git? Yes, the git you're already familiar with. The code buddy,...
Collapse
 
peledzohar profile image
Zohar Peled • Edited

When working in a team with multiple developers on the same project, I find it best to also include your first name on the branch name itself - assuming at any given point of time there are at least 4-10 active branches (depending on the size of the team and product), it really helps a lot to find it. My personal favorite convention: <FirstName>_<TaskNumber>_shorten-task-name - so Zohar_12345_Do-something

Collapse
 
shinjithdev profile image
shinjith

Couldn't agree more! When I worked on complex web projects, there were typically a dozen or so branches created by different team members for various interfaces and other things. I used to include a short alias for myself at the end of a branch name.

Collapse
 
_ndeyefatoudiop profile image
Ndeye Fatou Diop

Same here! I generally do « nfdiop/branch-name »

Collapse
 
wrp profile image
William Pursell

The notion of embedding metadata about the commit in the subject (ie "conventional commits") is misguided. Rather than putting such data in the subject line, stash it in a trailer. wrp.github.io/blog/2024/01/03/conv...

Collapse
 
shinjithdev profile image
shinjith

The notion of embedding metadata about the commit in the subject (ie "conventional commits") is misguided.

I don't think so; as I indicated in the conclusion, the conventional commit is not an obligatory practice; you can follow it in any way/extent you feel comfortable. If you are concerned about keeping consistency, I believe that is a small price to pay. The benefits of clean, organized commit histories and automated, rich changelogs and much more, feels significant to me.

Rather than putting such data in the subject line, stash it in a trailer.

Conventional commit already encourages including thorough explanation in the commit message body; formatting that is also a smart choice to match requirements like the tools you're talking about.

In the end, it's only individual or organizational choice to conform to such standards, and I never thought these conventions were mistaken or unnecessary.

Collapse
 
wrp profile image
William Pursell

I think you may be missing my point. I agree whole-heartedly that a clean, organized commit history is a desirable goal. Indeed, that is the entire reason that I discourage the practice of including meta-data in the subject line and instead encourage people to put that data in commit trailers. When projects attempt to place meta-data in the subject line, the formatting will inevitably be inconsistent.
By placing that data in trailers instead, there is some support from the tooling to ensure consistency and you gain a great deal of flexibility in viewing the data.

Thread Thread
 
shinjithdev profile image
shinjith

I apologize for misinterpreting your message. I appreciate that you are recommending a better technique than adding all meta data in the subject line; that is true. I believe we should introduce a modern or better approach to do this with solid reasoning, as many people still follow traditional methods.

Collapse
 
heetvekariya profile image
HeetVekariya • Edited

Great work !
My commit messages:

Image description

Collapse
 
shinjithdev profile image
shinjith

Looks great! Were you able to do it consistently?

Collapse
 
heetvekariya profile image
HeetVekariya

Well i enjoy writing good commit messages, and i am consistent from last HacktoberFest.

Collapse
 
kurealnum profile image
Oscar

This is a really informative post! I've just recently gotten in the habit of naming my branches correctly, but I didn't realize there were conventions for it.

Collapse
 
shinjithdev profile image
shinjith

I'm glad you found it informative, and I'll attempt to do a follow-up on this.

Collapse
 
adaptive-shield-matrix profile image
Adaptive Shield Matrix

How about Subject-First Commit Messages?
github.com/aaronjensen/software-de...

Collapse
 
shinjithdev profile image
shinjith • Edited

That is an interesting approach. This method is preferable when someone quickly checks the commit log for immediate information. Also, I believe it is preferable to put the type of commit as a prefix so that commits may be categorized with certain scripts or tools.

Anyway, it is a nice approach to describe, rather than lazy, on-the-go descriptions in the subject line.

Collapse
 
adaptive-shield-matrix profile image
Adaptive Shield Matrix • Edited

Every git tool in existance (gitlens, vscode, Github, Jetbrains, etc) only displays the first line of a git commit message. There are some esoteric git options to show more but in practice nearly no one bothers to use and remember them.
Very rarely do you go through all the git commit text.

Being able to quickly summary that the commit is about is much more valuable. If you want to you can still include tags for automated ci, ticket number or a description afterwards.

Summary: subject first approach is the best one.

Thread Thread
 
shinjithdev profile image
shinjith • Edited

Agreed. Subject-first practice is a smart technique. I read in many places that a commit description should be able to complete this sentence:

If applied, this commit will your subject line here

I believe this is one of the reasons to use imperative mood in description.

Since you stated this, I also think subject-first is better; perhaps we should renovate these practices.

Collapse
 
dikamilo profile image
dikamilo

Branch names: ticket ids with mix of git flow
Commit messages: conventional commits with descriptive messages - "Fix bug #67" means nothing and have zero value if you want to generate changelogs.

Collapse
 
shinjithdev profile image
shinjith

Sorry if you didn't find it useful! Maybe there are projects where this approach might help, such as for a project for tools that used by developers, in such cases referring issue number is enough or in similar situations.

Collapse
 
polcontreras profile image
Paul Contreras

This post is so good for improve our practices! cool

Collapse
 
shinjithdev profile image
shinjith

Thank for you appreciation!

Collapse
 
ritikbanger profile image
Ritik Banger
Collapse
 
shinjithdev profile image
shinjith • Edited

Great post!

Collapse
 
jangelodev profile image
João Angelo

Hi Shinjith ! Thanks for sharing

Collapse
 
shinjithdev profile image
shinjith

You're welcome! I'm glad you found the post interesting.

Collapse
 
akabia profile image
Aziz

I enjoyed this read and do agree that your points are effective disciplines for beginner and veteran developers to practice.

Collapse
 
shinjithdev profile image
shinjith

I'm glad you found this post informative and helpful!

Collapse
 
daveragos profile image
Dawit Beyene

you got some typos in there buddy

Collapse
 
shinjithdev profile image
shinjith

Loll! Too lazy to fix them, thought everyone would ignore :/. But atleast we can confirm this isn't AI generated

Collapse
 
hilleer profile image
Daniel Hillmann

It's interesting you recommend inducing ticket number, e.g. for Jira, as to me it provides absolutely no value for the branch name (and browsing those). Tracking the issue is not limited to be included in the branch name; it can just be included in a commit message, the PR title or the PR description.

When going through branch names, who will find any value of something like feature/SHOP-123 or get a clue of the branch's purpose?

Collapse
 
shinjithdev profile image
shinjith • Edited

Like I mentioned, I merely adapted & organised information from different references. I didn't try that approach where we use the ticket number like you said.

But maybe this is applicable to some project with strict handling of errors/bugs or so. I believe there might be some use cases for the same. I'm sorry if you didn't find it useful.