Use feature branches
A feature branch is any branch created to develop a specific feature or fix a specific bug.
Create a new branch for each feature or bug fix instead of working directly on the main
branch.
Merge feature branches into main
only when the feature is complete and tested. This keeps your work isolated and makes it easier to manage changes. With this approach, the main
branch always contains stable code that is ready for production.
Suppose you have been tasked with implementing Facebook login in the client application.
- You start working on the feature right away, committing your changes directly to the
main
branch. By the end of the day, you have a few commits that partially implement the feature. - Suddenly, you get a call from your boss, asking you to fix a critical bug with the existing login system and release it immediately.
- While the fix itself is trivial, you realize that if you release now, your half-finished Facebook login code will also be included in the release. Oopsie!
It gets even worse when working in a team. Imagine everyone working on the
main
branch: half-baked, in-progress code from different developers gets mixed together, making it impossible to figure out what is ready for release and what is not. This leads to confusion, broken builds, and wasted time.
- You create a new branch called
feature/facebook-login
(or whatever) and start working on the feature there. You commit your changes to this branch, keeping themain
branch clean and stable. - If tasked with unexpected bug fixes, you switch to the
main
branch, create another branch calledbugfix/login-issue
, and implement the fix there. Once tested, that bugfix branch is merged intomain
. The release is deployed, everyone is happy. - You switch back to your
feature/facebook-login
branch, do agit rebase
to incorporate the latest changes frommain
into your feature branch, and continue working on the feature. When the feature is complete, you merge it back intomain
.