Git Best Practices

Git is, by far, the most widely used version control system on the planet. With this tool, developers can collaborate more easily and work more efficiently. Git simplifies tracking code changes, provides speed, data integrity, and support for distributed non-linear workflows. 
In short, Git takes what could otherwise be chaos in a developer's life and structures it so that the project is easier to manage and more efficiently implemented.
If you and your team are just starting to use Git, you'll be thrilled. And if you've been using Git for a while, you may have worked with a team that doesn't use this tool effectively. Regardless of where you are in your Git journey, there are best practices to follow every step of the way. By following these tips, you'll not only significantly improve your Git experience but also make collaboration smoother and more efficient.

Let's look at some of these best practices. They apply regardless of the language in which you use Git. So whether you're a Java, JavaScript, .NET, C++, Ruby, Python, or PHP developer, working alone or on an enterprise-level team, these best practices will be applicable.

Single-Purpose Commits

A commit is an operation that sends the latest code changes to the source code repository. A commit can be anything. For example, you find a typo in the code. You fix the typo and create a commit. Commits make it easier for the team to understand what you did with the latest version of the code. One pitfall is not committing until a number of changes have been made, and then sending a commit that covers all those changes. When you do that, it makes code review less effective. Instead, create a commit for a single purpose, which:

  • Improves code review efficiency.
  • Simplifies rolling back code to a previous state.
  • Tracks changes in the ticketing system.
  • Simplifies viewing the git log.

Write Informative Commit Messages

A very important feature of commits is adding messages to them. These messages should be informative so that your team has a clear understanding of what you did in the commit. If you just add a generic or meaningless commit message, you're not helping the team. When you create a commit, be sure to add an informative message that clearly indicates what work you performed. It should be concise yet contain enough information so that everyone involved understands exactly what you did. A useful commit might look like this:

feature: added function X

^--^ ^---------------^
| |
| +-> Short description of the newly added function.
|
+-------> Type of feature added in the commit.

You should also be consistent in your commits. Your team should develop a style that makes it easy to write these comments and also easy to understand at a glance what was done in the commit.

Commit Early and Commit Often

This relates to single-purpose commits. You need to start adding your commits from the beginning and at regular intervals (most likely every time you make changes to the code in the repository). Don't get out of the habit of committing, otherwise you won't be able to follow the best practice of single-purpose commits and will confuse your team or make them work more than necessary.

Squash Multiple Commits

At some point, you'll find that too many single-purpose commits can make working with the Git repository difficult. Think of it this way: if you have 100 commits for each branch you create, that can lead to a huge number of commits. So instead of leaving all those individual commits as they are, squash them using the git rebase command. This will open an editor with a list of commits and the ability to act on them. The goal is to make your repository as informative and clean as possible.

Never Change History

After pushing a commit, you should not change it in the project history. Doing so makes nearly impossible to maintain consistent version control. So even if you are tempted to use git rebase to remove commits, only do so on branches that you are not using. The branch you are working on should always remain with its full history (and commits). This is true even if you made a big mistake in a commit. Fix that mistake and create another commit so everyone knows the issue is resolved.

Using Tags

A tag is a snapshot of a branch's state at a specific point in time. Think of a tag as a named pointer to a particular commit. This is very useful when you need to mark an important point in the project history. For example, you can mark release points like v1.0, v1.2, or v2.0. Tags make this easy. Git allows you to create both lightweight and annotated tags. Lightweight tags are like branches that don't change, while annotated tags are stored in the Git database as full objects. Annotated tags can also contain a checksum, tagger name, email, date, tag message, and can be signed and verified using GNU Privacy Guard (GPG). The most common tag is the annotated tag.

Conclusion

Git is a tool that every software developer should use for version control. And if you are using Git, it's important to set it up and follow best practices to ensure that your work is painless and efficient.