Introduction to Branching
What Will We Learn?
- What a Git branch actually is under the hood
- Why branches are lightweight and fast to create/switch
- How branching keeps your
mainbranch safe from unfinished work - Real world scenarios where branching is essential
- Best practices to follow when working with branches
What is Branch?
At its core, a branch is just a lightweight movable pointer that points to a particular commit. Whenever you make a new commit, the branch pointer moves forwards automatically with it to point the new commit.
When you initialize your project with git init, it creates a new default branch master. Well this is not any special branch compare to the others but developers treates this default as the main branch and some developers may rename master to main too to denote that this default branch is main branch of the project.
What Makes Branches Standout?
The lightweight feature of branch makes it extremely fast to create and switch. Branch in Git is simply a file that contains 40 character SHA-1 hash of the commit it points to and a newline. This means creating branch is as quick as writing 41 bytes to a file (40 char SHA-1 hash + newline).
This is not only feature that makes Git standout. Actually, when you checkout or switch to any branch, Git revers the files in your working directory back to the snapshot (commit) the branch pointer to. This means any changes you introduce in your new branch won’t affect files in your main branch. You need to manually merge it. This is what makes the independent development in any feature branch without even touching main branch.
Real World Use Cases
Branches aren’t just a Git feature for show, they solve real problems that come up daily in a development team. Here are some common scenarios:
Feature Development: Say you’re building a new payment gateway integration. Instead of writing code directly on main and risking breaking the live product, you create a feature/payment-gateway branch. You can commit as often as you like, experiment, even leave things half-broken overnight, without affecting anyone else’s work.
Bug Fixes in Production: Imagine your app is live and a critical bug is reported. Meanwhile, your main branch already has half-finished work for the next release sitting in it. You create a hotfix/login-crash branch straight from the last stable commit, fix the issue there, and merge it back without dragging in the unfinished feature work.
Experimentation: Want to try out a new library or refactor a core module but not sure if it’ll pan out? Create a throwaway branch like experiment/new-orm. If it works, merge it in. If it doesn’t, just delete the branch, no harm done to your actual codebase.
Parallel Team Collaboration: In a team of five developers, each person can work on their own branch (feature/search, feature/notifications, feature/user-profile) simultaneously, without stepping on each other’s toes. Everyone merges into main only when their work is tested and ready.
Code Review Workflow: Most teams don’t push straight to main. Instead, a branch is created, work is done, a Pull Request (PR) is opened against main, teammates review the code, and only after approval does it get merged. Branches are the backbone that makes this whole review process possible.
Best Practices With Branching
Keep branches short-lived: The longer a branch stays open, the more it diverges from
main, and the more painful the eventual merge becomes. Try to merge and delete branches as soon as their purpose is done.Use descriptive, consistent naming: Names like
feature/add-search-filter,bugfix/null-pointer-checkout, orhotfix/api-timeouttell anyone what the branch is for at a glance. Avoid vague names liketest1ormybranch.Never commit directly to
main: Treatmainas sacred. All changes should flow through feature or bugfix branches and get merged via review, not pushed directly.Pull latest changes before branching: Always branch off an up-to-date
main(git pull origin mainfirst) so you’re not building on stale code.Delete merged branches: Once a branch is merged, delete it (
git branch -d branch-name). A repository cluttered with dozens of old branches makes it hard to tell what’s active and what’s dead weight.Commit often, but keep commits meaningful: Small, focused commits within a branch make it easier to review changes and, if needed, revert a specific change without undoing everything.
Rebase or merge, know the difference: Some teams prefer
rebaseto keep history linear, others prefermergeto preserve the exact history of when branches diverged and came together. Pick one strategy as a team and stick with it consistently.Protect your
mainbranch: Most Git hosting platforms (GitHub, GitLab, Bitbucket) let you set branch protection rules, requiring PR reviews, passing tests, or approvals before anything can be merged intomain. Use this to enforce discipline automatically instead of relying on trust alone.