Why you need to maintain your git commits small and significant – Donny Wals

Why you need to maintain your git commits small and significant – Donny Wals


Printed on: February 19, 2025

Whenever you’re utilizing Git for model management, you are already doing one thing nice in your codebase: sustaining a transparent historical past of adjustments at each cut-off date. This helps you rewind to a secure state, monitor how your code has developed, and experiment with new concepts with out absolutely committing to them straight away.

Nevertheless, for a lot of builders, Git is simply one other instrument they’ve to make use of for work. They write a whole lot of code, make commits, and push their adjustments with out giving a lot thought to how their commits are structured, how huge their branches are, or whether or not their commit historical past is definitely helpful.

Why Commit Hygiene Issues

As tasks develop in complexity and as you achieve expertise, you may begin seeing commits as greater than only a step in pushing your work to GitHub. As a substitute, commits change into checkpoints—snapshots of your venture at particular moments. Ideally, each commit represents a logical stopping level the place the venture nonetheless compiles and capabilities accurately, even when a characteristic isn’t absolutely applied. This fashion, you at all times have a dependable fallback when exploring new concepts or debugging points.

Now, I’ll be sincere—I’m not at all times excellent with my Git hygiene. Generally, I get deep into coding, and earlier than I notice it, I ought to have dedicated ages in the past. When engaged on one thing important, I attempt to stage my work in logical steps in order that I nonetheless have small, significant commits. If you happen to don’t do that, the results might be irritating—particularly in your teammates.

The Ache of Messy Commits

Think about you are debugging a problem, and also you pinpoint that one thing broke between two commits. You begin wanting on the commit historical past and discover one thing like:

  • wip (Work in Progress)
  • fixing issues
  • extra updates

None of those let you know what really modified. Worse, if these commits introduce massive, sweeping adjustments throughout the codebase, you’re left untangling a multitude as an alternative of getting useful insights from Git’s historical past.

How Small Ought to Commits Be?

An excellent rule of thumb: your commits needs to be small however significant. A commit doesn’t must symbolize a completed characteristic, nevertheless it needs to be a logical step ahead. Usually, this implies:

  • The venture nonetheless builds (even when the characteristic is incomplete).
  • The commit has a transparent function (e.g., “Refactor JSON parsing to make use of Decodable”).
  • If you happen to’re including a operate, think about including its corresponding check in the identical commit.

For instance, let’s say you’re refactoring JSON parsing to make use of Decodable and updating your networking shopper:

  1. Commit 1: Add the brand new operate to the networking shopper.
  2. Commit 2: Add check scaffolding (empty check capabilities and vital recordsdata).
  3. Commit 3: Write the precise check.
  4. Commit 4: Implement the characteristic.
  5. Commit 5: Rename a mannequin or refactor unrelated code (as an alternative of bundling this into Commit 4).

By structuring commits this manner, you create a transparent and comprehensible historical past. If a teammate must do one thing comparable, they will have a look at your commits and observe your course of step-by-step.

The Stability Between Clear Commits and Productiveness

Whereas good commit hygiene is necessary, don’t obsess over it. Some builders spend as a lot time massaging their Git historical past as they do writing precise code. As a substitute, try for a steadiness: maintain your commits clear and structured, however don’t let perfectionism sluggish you down.

You actually don’t have to select aside your adjustments simply so you may have the cleanest commits ever. For instance, in case you’ve mounted a typo in a file that you simply have been engaged on, you don’t have to make a separate commit for that if it means having to stage particular person traces in a file.

Alternatively, if fixing that typo meant you additionally modified a handful of different recordsdata, you may need to put some additional work into splitting that commit up.

Commit Messages: Crafting a Significant Story

Along with the scale of your commits, your commit messages additionally matter. An excellent commit message needs to be concise however informative. As a substitute of imprecise messages like repair or up to date code, think about one thing extra descriptive, like:

  • Refactored JSON parsing to make use of Decodable
  • Fastened reminiscence leak in caching logic
  • Added unit check for community error dealing with

By protecting your commit messages clear, you assist your self and others perceive the development of adjustments with out having to dig into the code.

Rewriting Git Historical past When Needed

Generally, chances are you’ll need to clear up your Git historical past earlier than merging a department. That is the place instruments like interactive rebase come in useful. Utilizing git rebase -i HEAD~n, you may:

  • Squash a number of small commits into one.
  • Edit commit messages.
  • Reorder commits for higher readability.

Nevertheless, be cautious when rewriting historical past—as soon as commits are pushed to a shared department, rebasing could cause conflicts in your teammates.

Rebasing on the command line might be tough however fortunately most GUIs could have methods to carry out interactive rebasing too. I personally use interactive rebasing so much since I like rebasing my branches on predominant as an alternative of merging predominant into my options. Merge commits aren’t that helpful to have in my view and rebasing permits me to keep away from them.

In Abstract

In the long run, it’s all about ensuring that you find yourself having a paper path that is smart and that you’ve a paper path that may really be helpful when you end up digging by way of historical past to see what you probably did and why.

The truth is, you gained’t do that typically. However whenever you do, you’ll really feel glad that you simply took the time to maintain your commits lean. By protecting your commits small, writing significant messages, and leveraging Git’s highly effective instruments, you make sure that your model management historical past stays a helpful useful resource somewhat than a tangled mess.

Leave a Reply

Your email address will not be published. Required fields are marked *