DEV Community

Swapnil V. Patil
Swapnil V. Patil

Posted on • Originally published at Medium on

10 Git Best Practices to Start Using in Your Next Git Commit


Conflict?

Don’t we all eventually find ourselves wondering…?

  • What aims does this code pursue?
  • How did this bug get there?
  • How should I start working on this sizable pull request?

Code comments, style guides, and documentation are a few solutions to these problems, and we all, at some point, have unavoidably wasted hours trying to comprehend code.

Git commits are more than just saved points or logs of every single step in a larger project. A stable and collaborative open or closed-source project depends on high-quality Git commits.

Let's discuss some pointers to enhance and streamline your development process. So without further ado, let’s get started.

What do commits mean 🤷‍♂️

Commits are periodic & logical changes; Ideally, each change is the smallest possible unit in the git repository. The history of your repository and how it came to be the way it is now should be chronicled throughout time through commits.

To put it simply, commits are a first-person historical record of precisely how and why each line of code was created.

Commits work best when they are polished and adjusted to specifically address their target audiences, which may include reviewers, other contributors, and even your future self.

The Anatomy of a Commit Message 🫀

Basic:

git commit -m <message>
Enter fullscreen mode Exit fullscreen mode

Detailed:

git commit -m <title> -m <description>
Enter fullscreen mode Exit fullscreen mode

Writing Better Commits 👍

Capitalization and Punctuation 🅰️

Put the first word in capital letters and omit the period in the end. Keep in mind to utilize just lowercase letters while utilizing traditional commits.

Mood 🌚

In the subject line, use the imperative mood. When you use the imperative mood, you convey that you are issuing a command or request.

Type of Commit 🥸

Indicate the kind of commit. A set of phrases to explain your consistent modifications across the board is advised and much more helpful.

Length 🍢

The body should be limited to 72 characters, and the opening line should ideally not exceed 50 characters.

Content 📃

Be concise and attempt to avoid using extraneous words and phrases.

Structure the narrative 🪶

The “story” of your modification is contextualized with the code using the narrative framework of a series of commits. Your project’s history should be depicted in the commits. It ought to be your project’s biography.

Commits that are disjointed and lack a straightforward narrative will impact both the reviewer and the developer.

The reviewer will have to switch contexts if those commits don’t provide a straightforward narrative since they move from subject to subject.🥴

Rearrange your commits to correspond to your narrative’s plan.

Huenry Hueffman 🔜 Reboot Blue on Twitter: "I fucked up Git so bad it turned into Guitar Hero pic.twitter.com/vUKZJAQKWg / Twitter"

I fucked up Git so bad it turned into Guitar Hero pic.twitter.com/vUKZJAQKWg

Resize and stabilize the commits ⚛️

The code in each commit generates software, even though the structure of a commit series might describe a feature’s high-level history. Even though code can be pretty sophisticated, people must comprehend it to work together. Make each commit “atomic” and “short,” then.

A small commit only does one “thing” and has a limited reach. When a commit is a reliable, standalone change unit, it is atomic.

Explain the context ❓

Code in commits is only one aspect of what they are. Although valuable, commit messages are frequently ignored commit components. In the commit message, please explain what you’re doing and why you’re doing it.

What readers need to grasp should be communicated in a committed message’s content.

Commit Early & Often ⌚

Early and frequent commitments help you commit just related changes and keep your commits minimal. Additionally, it enables you to avoid merge conflicts and share your code more frequently with others.


I know Git, depending on what you mean by “know”.

Conventional Commits 📜

Conventional commit is a formatting standard that offers a set of guidelines for creating a constant commit message structure.

The following is a list of possible commit types:

feat

  • Title: Feature
  • Description: A completely new feature
  • Emoji: ✨

fix

  • Title: Bug Fix
  • Description: A bug fix
  • Emoji: 🐛

docs

  • Title: Documentation
  • Description: Changes only to the documentation
  • Emoji: 📚

style

  • Title: Styles
  • Description: Alterations that don’t change the code’s intent(white space, formatting, missing semi-colons, etc.)
  • Emoji: 💎

refactor

  • Title: Code Refactoring
  • Description: An update to the code that neither adds features nor fixes bugs
  • Emoji: 📦

perf

  • Title: Performance Improvements
  • Description: A performance-enhancing tweak to the code
  • Emoji: 🚀

test

  • Title: Tests
  • Description: Addition of tests or updating of tests
  • Emoji: 🚨

build

  • Title: Builds
  • Description: Modifications to the build system or external dependencies
  • Emoji: ⚒️

ci

  • Title: Continuous Integrations
  • Description: Modifications to our CI configuration scripts and files
  • Emoji: ⚙️

chore

  • Title: Chores
  • Description: Other updates that don’t alter the test or src files
  • Emoji: ♻️

revert

  • Title: Reverts
  • Description: Reverts a previous commit
  • Emoji: 🗑

Now, let’s see some commit aliases:

initial

  • Maps to: feat
  • Title: Initial
  • Description: Initial Commit
  • Emoji 🎉

dependencies

  • Maps to: fix
  • Title: Dependencies
  • Description: Update dependencies
  • Emoji: ⏫

peerDependencies

  • Maps to: fix
  • Title: Peer dependencies
  • Description: Update peer dependencies
  • Emoji: ⬆️

devDependencies

  • Maps to: chore
  • Title: Dev dependencies
  • Description: Update development dependencies
  • Emoji: 🔼

metadata

  • Maps to: fix
  • Title: Metadata
  • Description: Update metadata (package.json)
  • Emoji: 📦

Good Commits VS Bad Commits ⚡


Never miss a commit

Good 👍

git commit -m “Add margin to nav items to prevent them from overlapping the logo”

♻️ chore: update npm dependency to the latest version

✨ feat: set the background color to red to improve contrast

🐛 Fix bug redirecting users to 404 page

Bad 👎

git commit -m “ Add margin”

lazy loading

dependency updated

Set bg to red

endgame

deleted all the files

Style changed

Additional Resources 🌟

  • git-absorb: A tool that divides random fixes into allocated fixups automatically commits
  • Commitizen: Its primary goal is to establish a uniform method for committing rules and disseminating them via the Commitizen CLI.
  • Git Commit Message Emoji List

Thank you so much for reading!

Happy coding!


Top comments (0)