HomeiOS DevelopmentWhy it is best to hold your git commits small and significant...

Why it is best to hold your git commits small and significant – Donny Wals


Printed on: February 19, 2025

Once you’re utilizing Git for model management, you are already doing one thing nice to your codebase: sustaining a transparent historical past of modifications at each time limit. This helps you rewind to a secure state, observe how your code has advanced, and experiment with new concepts with out totally committing to them immediately.

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

Why Commit Hygiene Issues

As initiatives develop in complexity and as you acquire expertise, you may begin seeing commits as greater than only a step in pushing your work to GitHub. As an alternative, commits change into checkpoints—snapshots of your undertaking at particular moments. Ideally, each commit represents a logical stopping level the place the undertaking nonetheless compiles and features accurately, even when a characteristic isn’t totally carried out. This manner, you all the time have a dependable fallback when exploring new concepts or debugging points.

Now, I’ll be sincere—I’m not all the time excellent with my Git hygiene. Typically, 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. For those who don’t do that, the results will be irritating—particularly to 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 inform you what really modified. Worse, if these commits introduce giant, sweeping modifications throughout the codebase, you’re left untangling a large number as an alternative of getting useful insights from Git’s historical past.

How Small Ought to Commits Be?

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

  • The undertaking nonetheless builds (even when the characteristic is incomplete).
  • The commit has a transparent goal (e.g., “Refactor JSON parsing to make use of Decodable”).
  • For those who’re including a operate, take into account including its corresponding take a look at 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 take a look at scaffolding (empty take a look at features and vital recordsdata).
  3. Commit 3: Write the precise take a look at.
  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’ll take a look at your commits and comply with your course of step-by-step.

The Steadiness 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 an alternative, try for a stability: hold your commits clear and structured, however don’t let perfectionism sluggish you down.

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

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

Commit Messages: Crafting a Significant Story

Along with the dimensions of your commits, your commit messages additionally matter. A very good commit message needs to be concise however informative. As an alternative of obscure messages like repair or up to date code, take into account one thing extra descriptive, like:

  • Refactored JSON parsing to make use of Decodable
  • Mounted reminiscence leak in caching logic
  • Added unit take a look at for community error dealing with

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

Rewriting Git Historical past When Obligatory

Typically, chances are you’ll wish 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.

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

Rebasing on the command line will be difficult however fortunately most GUIs can have methods to carry out interactive rebasing too. I personally use interactive rebasing loads since I like rebasing my branches on major as an alternative of merging major 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

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

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

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments