Git is a popular version control system used by many software developers and web programmers all over the world. It offers many features that makes it easy to for distributed teams to work on large projects. But ultimately, every code repository depends on the discipline of its developers.
If people are allowed to work as they want, it will make the code repository bloated and eventually unmanageable. So it is always to abide by certain best practices so that the repository shapes up well and simplifies everyone’s life. In this article, we will be looking at some of the best practices that you can easily apply to your projects. Of course, there are innumerable things you can do to optimize your code development. But these few ones here will help you get to a good start.
Why Need Git Best Practices
You may be wondering that git is already a very robust tool with tons of commands and gives you the ability to navigate almost any software development situation. This is because coding is basically a discipline and required coders to adhere to a set of rules, to make it easy for everyone to collaborate. If some coders don’t play by the rules, then it makes things difficult for everyone on the team. Git best practices allow you to streamline repository management, adding new features and fixing bugs. It also prevents your code from getting bloated and difficult to manage. Also, it allows you to maintain your codebase in case people leave your team, new people join the team or users transition from one role to another.
Git Commit Best Practices For you
Here are some of the simple best practices you can easily adopt in your projects.
1. Commit Often
This is one of the most common best practice used by almost every git developer. It is always good to keep committing a small number of code changes instead of something that involves a large number of changes. This allows you to easily rollback to a previous commit if required. It also allows software developers to easily add changes to code base and share them with others, without merge conflicts.
2. Commit Related Changes
A commit should cover one or more related changes. This will help everyone understand the changes made in a particular commit, the files that were added/modified, and also design tests that only check those specific changes. For example, if you need to add 2 features or fix 2 bugs, then they need to be separated as 2 commits. This also adds a certain level of compartmentalization to your code, making it easy to maintain.
3. Do not bundle multiple changes
Sometimes developers commit changes after a long time, with many changes, features and bug fixes. This very confusing for any one reading the commit because it becomes difficult to understand which changes are related to which features/bug fixes, which files were modified, etc.
4. Testing Before Commit
Many developers commit changes without properly testing them. Sometimes they think that they will start testing after the commit. Whenever we test, invariably we will discover bugs and need to fix them. If you have already committed changes before testing, then you will need to commit additional changes after bug fix. This makes it difficult for others to understand your code because first they need to go through the earlier commit which had bugs. Then they need to understand the bug fixes you have committed, and put both things together. It results in unnecessary waste of time.
5. Commit Completed Work
Sometimes software developers commit half-baked changes, without completing them. Make sure to break down each feature/bug fix into logical chunks that can be completed quickly as well as tested. Also, it should be easily understood by others. If you are concerned about having too many uncommitted changes then you can use ‘git stash’ command to temporarily stash them. When you are ready to commit, just use ‘git stash apply’ command to bring them back to the tracked changes.
6. Intuitive Commit Messages
Every time you commit changes to your software repository, you are required to add a message along with it. The primary purpose of this message is to make it clear to everyone using this repository, the changes you’ve made in this particular commit. You can describe the changes in a single line or using multiple lines with line breaks.
So you can start with a one line summary of the changes made in the commit. You can follow it with a list of points about specific changes. You are free to expand any of these changes into paragraphs if you need to go into details. There is no fixed format for this and you can customize it as per your convenience. Also, there is no character limit for the message.
However, many developers often use 50-72 character limit for title summary. This is followed by a blank line. Then they mention the details. Nevertheless, it is always good to be concise.
7. Use a Workflow
A git workflow is a set of rules, processes and conventions used by teams to manage code repositories. It depends on project’s team, size and complexity. For example, some teams may choose to use long branches whereas some may like to use topic-based branches, and so on. It is important to research about it from other teams, brainstorm about it with your own team mates, and most importantly, document & share it within your team. You may also need to train your team to use the agreed workflow. Git workflows make it easy to easily and consistently make changes to a code repository.
8. Use Branches
Git makes it easy to quickly branch your code and continue working on it, without disturbing the existing code base. Each branch supports multiple commits. When you are ready to integrate the branch into main code, you can simply merge it. In every git repository, master is the main code branch. Generally, small projects use two branches – working and master. Working branch contains code in development.
There are several ways to create git branches. You can branch code based on stage of development (coding, testing, etc.), or features, or bugs, or something else. You can also create sub-branches for each of these branches. When you merge a branch or sub branch, then git will automatically merge all its commits.
Conclusion
In this article, we have learnt about some of the most commonly used best practices for git development. There are numerous practices but we have covered the popular and easy ones. You can customize them as per your project requirements. Even if your project is running smoothly, it is important for team leads and project managers to keep an eye on productivity, take feedback from your software developers and use git best practices to simplify their lives.
Also read:
How to Check if Variable is Defined
How to Check if String is a Valid Number
How to Move Element into Another Element in jQuery

Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI & software development.