The Talent500 Blog
Git commit practices

7 Best Git Practices For Your Next Commit

Git is one of the most widely used modern control versions in the world. Developed in the year 2005 by Linus Torvalds, the creator of the Linux operating system Kernel, Git is a mature open source project that is being actively maintained by developers across the world.

Today, a large number of developers rely on Git for version control for their projects. These projects include both commercial and open source. But what is the real reason behind the success of Git? Apart from ease of operation, its the well-defined guidelines or best practices, which must not be skipped.

Checking in code is a crucial step in any version control system. In Git, this is known as Git commit. Besides testing, commit allows individual developers to contribute to an expansive and evolving codebase. But all commits are not the same, and bad check-ins lead to regression, delays and broken build, increasing the time to market. Below, we have listed some proven git commit best practices for higher quality check-ins that prevent delays.

1. Commit Early & Often

If you are new to Git, follow the principle of ‘commit early and often’ to keep track of your changes and streamline your code development process. So, each time you change something, commit – whether it is renaming a file or something even more mundane, don’t think but commit until it becomes second nature. 

2. Use a Branching Strategy

The correct branching strategy is vital to streamline your commit process. Branching improves the productivity of your team and helps various groups to work in parallel. In general, short branches with limited changes improve traceability and expedite code reviews. Smaller branches help to minimize bad check-ins, while committing more frequently eliminates confusion and prevents merging. It is also a good idea to have check-ins for a single purpose to improve traceability and catch bugs easily. There are many branching strategies to choose from, but whatever you choose, you must inform your team to bring them on the same page.

3. Test Your Code Before You Commit

Committing half-baked code will not cause you much harm, even though it isn’t good practice. Therefore, if you ‘think’ a task is complete but not sure whether it is done – do not commit. 

According to experts, you should only commit code when a logical component gets completed, and you are certain there are no side effects of implementing it. Especially if you are sharing code, it is your responsibility to make sure that it is well tested and completed in all aspects before committing to prevent problems for others. Also, if your reason to commit incomplete code is to create a clean working copy, we recommend that you try the Stash feature in Git instead, which is specifically meant for this purpose.

4. Write Good Commit Messages

Good commit messages are short, clear and easy to understand. Here are some tips to achieve this:

  • Always begin your message with a short summary of the changes you have made. This summary can be up to 50 characters for the sake of brevity. 
  • Leave a blank line before proceeding to the main message body. Here, you can provide detailed answers to questions like the reason for making the change or differences from the previous implementation.
  • It is recommended to write your message in the present tense or imperative, and use consistent commands as generated in Git. So, always write ‘Fix Bug’ and not ‘Fixed Bug’ or ‘Fixes Bugs’.
  • All subsequent paragraphs must come after blank lines. You may even use bullet points (generally hyphens or asterisks) preceded by a single space.
  • Always use a hanging indent for starting a new paragraph.

5. Make Small, Single-Purpose Commits

As mentioned before, it is always best to commit small sections of code so that every member of the team can understand what work has been completed. Besides, if something goes awry, it is always easier to fix smaller check-ins to maintain a stable codebase. Also, if separate policies govern your projects or codes, it is an excellent idea to keep them in distinct code lines for proper source code management and better housekeeping.

6. Avoid Rewriting Master’s History

Ever wanted to go back in time and change the bad code you have committed? Well, Git gives you the actual superpower to do that. In Git, you can go back to earlier code and rebase different branches on other branches. You also have the option to delete commits, rename them, or put them all together in a single commit. As an individual developer, you can access even more features, but we suggest that you are cautious in case you are using a shared system or work with the team. 

Remember, with great power comes more responsibility, and unless you are working solo, it is best not to rewrite code history. If your code is shared with a team, it is better to check out previous commits rather than altering them to save them for future reference. 

Besides saving history, it is also a good practice to maintain traceability. Therefore, every time you check-in a change, make sure that you include all the affected files and unit tests. This is even more important when you work between branches. This practice not only makes your changes traceable but also ensures others can use your check-ins without breaking their builds.

7. Fix a Workflow

Git offers users multiple workflows to choose from, such as long-running branches, git-flow, merge or rebase, topic branches, etc. What you choose amongst these depends on numerous factors, including your project scope, development goals, and the personal preferences of your team. However, you must select a workflow before you begin work so that the entire team follows the same format. By omitting this step, you may find different developers in your team using different workflows, which may lead to confusion and chaos.

To conclude, we would like to mention that the information shared on Git commit best practices above is by no means exhaustive. However, these seven practices will certainly help you in building a strong foundation in Git, despite your skill level. 

It goes without saying that you must continue learning and developing your own best practices to up your expertise further. If you are eyeing career growth, you may also list your profile on Talent500 to empower your job search and find a lucrative role with Fortune500 companies.

3+
Bhargavi

Bhargavi

Senior QA at Talent500. Loves to test all sorts of applications and enthusiastic to learn new technologies. Enjoys bike riding in her free time.

Add comment