Mastering Git Committing: Best Practices and Guidelines
Tags: git, github
Published: 2024-02-28 19:41:55

In the realm of version control systems, Git stands out as one of the most powerful and widely adopted tools. Yet, its effectiveness greatly depends on how well it's utilized, particularly when it comes to committing changes. Git commits are not just snapshots of code; they are the building blocks of collaboration and code evolution. Here, we delve into some best practices and guidelines for mastering Git committing.

> Best Practices for Commit Messages: Starting with Clear Prefixes. e.g. FIX, RELEASE, FEATURE, BUG, REFACTOR, TEST, BUILD etc


### Commit with Purpose
A pivotal aspect of Git committing is ensuring that each commit encapsulates a coherent set of changes. Whether it's fixing a bug or implementing a feature, commit granularity enhances clarity and facilitates efficient collaboration.

### Embrace Frequency
Committing frequently is akin to creating checkpoints in your development journey. By breaking down changes into smaller, manageable commits, developers promote seamless integration and mitigate the risk of conflicts.

### Shun Half-Baked Commits
Avoid the temptation to commit incomplete work. Each commit should represent a logically complete unit of work, ensuring that changes are robust and actionable.

### Validate Before You Commit
Prior to committing, rigorously test your code to ensure its integrity and functionality. While local commits offer leeway for experimentation, shared commits demand a higher standard of quality assurance.

### Craft Compelling Messages
The art of writing commit messages cannot be overstated. A well-crafted commit message serves as a narrative, guiding future developers through the evolution of code changes.

### Harness Branching Power
Branching is Git's cornerstone feature, empowering developers to explore new ideas and isolate changes without disrupting the main codebase. Embrace branches as a versatile tool in your development arsenal.

### Establish Workflow Consensus
Choose a Git workflow that aligns with your project's needs and team dynamics. Consensus on a common workflow fosters cohesion and enhances productivity.

## Practical Examples and Guidelines

Let's explore some exemplary commit messages that embody these principles:



**Example 1: Sort summery based commit**

```
Commit Hash: 3114a97ba188895daff4a3d337b2c73855d4632d
Author: [Abhishek]
Date: Mon Jun 11 17:16:10 2012 +0100
FIX: Changes has been done with the new c++ runtime module update
```


**Example 2: Description in multiple points**

```
Commit Hash: ae878fc8b9761d099a4145617e4a48cbeb390623
Author: [Abhishek]
Date: Fri Jun 1 01:44:02 2012 +0000

FEATURE: Done with processing module

- Minimized duplicated code for create.
- Modified wait_for_destroy to occur on shutdown instead of undefine.
- Enabled destruction of an instance while leaving the domain intact.
```




**Example 3: Large details**

```
Commit Hash: 31336b35b4604f70150d0073d77dbf63b9bf7598
Author: [Abhishek]
Date: Wed Jun 6 22:45:25 2012 -0400

RELEASE: Key features of this enhancement include:

- Identification and storage of guest arches in the permitted_instances_types list within the cpu_info dictionary of the host.
- Implementation of the arch filter to compare the instance arch against permitted instances types, filtering out invalid hosts.
- Addition of ARM as a valid arch to the filter, ensuring compatibility with diverse architectures.

Notably, the ArchFilter remains deactivated by default, offering flexibility in its utilization.
```

### Best Practices for Commit Messages: Starting with Clear Prefixes

When crafting Git commit messages, always beginning with a clear prefix enhances readability and organization. Let's explore some common prefixes and their significance:

Prefixes for Commit Messages

**FIX: Addressing Issues**

Starting a commit message with "FIX" indicates that it resolves a specific issue or bug within the codebase. These commits focus on improving the stability and functionality of the software by addressing existing problems.

**RELEASE: Version Updates**

Commits prefixed with "RELEASE" signify significant milestones in the project, such as the release of a new version or major feature set. These commits often include updates to version numbers, changelogs, and release notes.

**FEATURE: Introducing New Functionality**

Using "FEATURE" as a prefix denotes the addition of new features or functionalities to the software. These commits expand the capabilities of the application and contribute to its ongoing development.

**BUG: Fixing Bugs**

"BUG" prefixed commits indicate the resolution of specific bugs or issues. These commits focus on debugging and ensuring the smooth operation of the software by addressing reported problems.

**REFACTOR: Code Refactoring**

Starting a commit message with "REFACTOR" signifies restructuring or optimizing existing code without changing its external behavior. These commits aim to improve code quality, readability, and maintainability.

**DOC: Documentation Updates**

Prefixing commits with "DOC" indicates changes or additions to documentation, including README files, code comments, or developer guides. These commits enhance the clarity and accessibility of project documentation.

**TEST: Adding or Modifying Tests**

Using "TEST" as a prefix involves additions or modifications to test suites, ensuring comprehensive test coverage and validating the correctness of the software.

**STYLE: Code Style Changes**

Commits prefixed with "STYLE" involve updates to code formatting, styling, or conventions. These commits focus on improving the consistency and aesthetics of the codebase.

By starting commit messages with these clear prefixes, developers can streamline collaboration, improve code quality, and maintain a well-organized project history.


## Conclusion

In essence, Git committing is more than just a technical task; it's a strategic endeavor that shapes the evolution of code. By adhering to best practices and guidelines, developers can optimize collaboration, ensure code quality, and drive continuous improvement in software development.