git|July 10, 2020|3 min read

A Practical Guide on Understanding Git Best Practices

TL;DR

Follow Git best practices such as avoiding direct commits to master, using feature branches, writing meaningful commit messages, and keeping branches up to date.

A Practical Guide on Understanding Git Best Practices

Introduction

In this post, we will learn about some of Best practices while working in git.

I hope you have seen previous git training posts:

Don’t Do a direct commit in Master branch

Master branch is the default main branch in git. You should have your recently released code in git, which is like a replica of what is live. So that, any bug fix or any features can be built over it.

By directly committing, you are risking any urgent bug fixes. You should create other feature branches, test there and commit to master only when those changes aare ready to go live or went live.

Read more about one of Simplest Git Branching Strategy

Always take a pull before pushing changes

Before pushing your code, always first take a pull from remote branch. This is to make sure that your branch is up to date with the source branch. And, there will be no conflict at the time of push. By taking pull, you might need to resolve conflicts.

git pull origin master

Put restriction in who can commit to your Master or other branch

In git, you can put restriction on who can commit or merge code in your restrctive branches like master or other branch.

For more details see: Protect Git branch

Restrict that at least N number of code reviews are required before merging

Code reviews are most important aspect in managing code. Peer code reviews can discover hidden bugs or issues, and they are very effective. Study shows that almost 70% bugs are caught in code reviews, if done effectively.

For more details, how you can restrict, read Mandatory Code reviews

Do write a good commit message and description

You have written a beautiful code, and developed awesome feature. But, 1 year down the line, when you needed to visit your commit history. You wonder why the hell this commit exists.

A well written commit message always helps.

Clean your Commit History before Merging

When we create a Pull Request (PR), please clean it by git rebase command. This enables you to remove any unimportant commit ids into main branch. Your feature branch might have some commits like correcting typos, fixing compilation error. But, there is no benefit of putting this as commit history. You can merge this commit with other commits.

The idea is to merge unnecessary commit ids into one.

Have a thorough Code Review

Code reviews are very important. Do not blindly merge your PR into main branch. Get it reviewed by your peer. A second eye always helps. You might accidently put some files which you don’t want to commit.

Have a .gitignore File

Its important to have .gitignore file in your project. We might accidently add files which are not intended for commit. We usually have files like credentials, which can accidently get pushed.

Always review your files to push list

Don’t do a blind push. Always review the files which you are committing and pushing. Always have a look at the diff by using git diff. Sometimes, developers put some temporary code change for development which you don’t want to push.

Related Posts

A Practical Guide in understanding Git Branch and Conflict resolution during merge

A Practical Guide in understanding Git Branch and Conflict resolution during merge

Introduction In this guide, We will learn about branching, handling conflict…

A Practical Guide on how to work with Git Basic Commands and workflows

A Practical Guide on how to work with Git Basic Commands and workflows

Introduction In this guide, we will see git basic commands, and fundamentals of…

A Practical Guide on how to work with Git log command and history

A Practical Guide on how to work with Git log command and history

Introduction In this post, we will see ways to look at git history logs. For…

A Practical Guide on how to to create your own git command alias

A Practical Guide on how to to create your own git command alias

Introduction In this guide, We will learn on how to create some handy command…

A Practical Guide for better understanding Git Diff

A Practical Guide for better understanding Git Diff

Introduction In this guide, We will get basic understanding of various options…

Git - How to create a Pull Request with no history of commits

Git - How to create a Pull Request with no history of commits

Introduction If you working on a github project in a team. Consider you have…

Latest Posts

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Most developers use Claude Code like a search engine — ask a question, get an…

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Every office lobby has the same problem: a visitor walks in, nobody’s at the…

Server Security Best Practices — Complete Hardening Guide for Production Systems

Server Security Best Practices — Complete Hardening Guide for Production Systems

Every breach post-mortem tells the same story: an unpatched service, a…

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

If you’re a Senior Engineer (L5) preparing for Staff (L6+) roles at MAANG…

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF have been in the OWASP Top 10 for over a decade. They’re among the…

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

The OWASP Top 10 is the industry standard for web application security risks. If…