What are the best practices for committing in Git?


Following are the best practices for committing in Git −

  • Make small, single−purpose commits

Committing small sections of code helps the team to understand what work has been completed. It is easier to revert smaller commits and helps you maintain a stable codebase. A commit should act like a wrapper around a set of changes. This means that if you are fixing 2 distinct bugs, use 2 different commits. In other words, changes or code that are logically related should be part of a single commit.

  • Commit messages should be short and detailed

Commit messages should begin with a short summary of the change. The summary should be in present tense. The subject line should be capitalized and should not exceed 50 characters and followed by a blank line. The commit message should be clear and should explain exactly what you have done. It is recommended to use an editor to add a long description of commits. A long description helps you to explain your code better to other team members. If you type git commit without using the −m flag, Git will open the default editor, where you can add a description. The following screenshot demonstrates this.

  • Test your code before you commit

It is necessary to test and review your code before committing to ensure that the quality of the codebase is not compromised. The code should be tested often and committed once.

  • Commit often

Make a commit as and when you complete a smaller unit of code that can be logically grouped. Frequent commits ensure that changes are integrated regularly and help to avoid merge conflicts. It is also important that the code is tested and reviewed for bugs before it is committed.

  • Use Branches

Branches are an independent line of development and a powerful feature tool by Git. Branches help you to maintain a distinct version of your code and avoid mixing up different lines of development. Do not push code straight to the master branch. Create and maintain a separate branch for different development workflows. For E.g., create separate branches for new features, bug fixes, ideas etc.

  • Configure commit authorship

Set your name and email address correctly when you commit. These details will help you to know who made a specific change. This information helps you in the future, if you want to ask the author about the business contexts or technical purpose of the code. It will also help to accelerate a bug fix, or trace the commit that caused a bug is present in the codebase.

  • Agree on a workflow

Git workflow defines a standard process for code collaboration using Git that should be followed within the team. There are various types of Git workflows and it is important to choose a workflow that best suits your project type. It is important that the team discusses and agrees upon a workflow and adheres to it.

Updated on: 20-Feb-2021

180 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements