Branch: What is a Branch in Git?
Définition
A Git branch is an independent line of development that allows working on changes without affecting the main code. Each branch represents an isolated working context with its own commit history.What is a Git Branch?
A Git branch is a lightweight pointer to a specific commit in the project history. Technically, it is simply a 40-character file (the SHA-1 hash of the commit) stored in the .git/refs/heads/ directory. Despite this technical simplicity, branches are one of Git's most powerful concepts and form the foundation of collaborative software development.
When you create a branch, you create a new isolated workspace where you can experiment, develop new features, or fix bugs without affecting the main branch. Changes made on a branch exist only on that branch until they are explicitly merged into another.
At Kern-IT, every developer systematically works on dedicated branches. The main branch remains always stable and deployable, while feature branches allow developing freely without risking breaking the production application.
Why Branches Matter
Branches are what enable a team of developers to work effectively on the same project without stepping on each other's toes. They bring essential flexibility to the development process.
- Parallel work: multiple developers can work simultaneously on different features, each on their own branch, without interference.
- Change isolation: a branch isolates your changes from the rest of the project. If your experiment fails, simply delete the branch with no impact on the project.
- Production stability: by keeping the main branch clean and only integrating tested, approved code, you ensure the production code is always stable.
- Code review facilitation: each branch corresponds to a coherent set of changes that can be reviewed and discussed via a pull request.
- Release management: branches allow maintaining multiple software versions simultaneously (production branch, development branch, hotfix branches).
How It Works
Creating a branch in Git is instantaneous thanks to the lightweight pointer mechanism. The command git branch feature/new-module creates a new pointer referencing the same commit as the current branch. To switch to this branch, use git checkout feature/new-module or, with recent Git versions, git switch feature/new-module. The shortcut git checkout -b feature/new-module combines both operations.
Once on the new branch, each commit you make advances that branch's pointer, while other branches remain unchanged. Git maintains a special pointer called HEAD that indicates which branch you are currently on.
The command git branch with no arguments lists all local branches. git branch -a also displays remote branches. git branch -d branch-name deletes a branch that has already been merged, while git branch -D forces deletion even if the branch contains unmerged changes.
Naming Conventions
Branch naming is an important team convention for maintaining repository readability. At Kern-IT and across the industry, the following prefixes are widely adopted:
feature/for new features (e.g.,feature/user-authentication)fix/orbugfix/for bug fixes (e.g.,fix/login-redirect)hotfix/for urgent production fixes (e.g.,hotfix/security-patch)chore/for maintenance tasks (e.g.,chore/update-dependencies)refactor/for code refactoring (e.g.,refactor/database-layer)
A good branch name is descriptive, uses hyphens as separators, and remains concise. It should convey the branch's purpose at a glance.
Concrete Example
A Kern-IT project is under development with three developers. The first is working on feature/payment-integration to add a Stripe payment system. The second is on feature/admin-dashboard to create an administration dashboard. The third is fixing a critical bug on hotfix/invoice-calculation.
The hotfix is completed first and merged into main after review. The two developers on feature branches then pick up this fix by merging main into their respective branches. Each continues their work independently, and the feature branches will be merged one by one after validation. This parallel workflow is only possible because of branches.
Best Practices
- One branch per feature: each branch should have a clear and unique objective. Avoid catch-all branches that mix multiple unrelated changes.
- Short-lived branches: a branch that lives too long accumulates divergences from main and makes merging more difficult. Aim for branches lasting a few days at most.
- Delete merged branches: after a successful merge, delete the source branch to keep the repository clean. GitHub offers this option automatically after a PR merge.
- Synchronize regularly: pull the latest changes from
maininto your branch to avoid surprises during the final merge. - Protect the main branch: configure branch protection rules on GitHub to require reviews and CI tests before any merge into
main.
Conclusion
Branches are the backbone of collaborative development with Git. They enable parallel work, change isolation, and a stable main codebase. Mastering branches means mastering Git. It is an essential prerequisite for any developer working in a team who wants to contribute effectively to quality software projects.
Enable automatic branch deletion after merge in your GitHub repository settings. This prevents the accumulation of stale branches and keeps your repository clean with no extra effort. Combine this with git fetch --prune locally to clean up deleted remote references.