Git Branching Explained: How Developers Work on Features Without Breaking Main Code
When working on a software project, developers frequently need to add features, fix bugs, and experiment with new ideas.
But what happens when several changes are being developed simultaneously?
This is where Git branching becomes useful.
Git branches allow developers to work on separate lines of development without immediately changing the main branch.
Let's understand how it works.
1. What is a Git branch?
A Git branch is a movable reference to a commit.
Most repositories begin with a default branch commonly named main.
Developers can create additional branches for specific tasks.
For example:
main
feature/login
feature/dashboard
bugfix/registration
Each branch can contain its own commits.
2. Create a new branch
To create and switch to a new branch:
git switch -c feature/login
This creates a branch named feature/login and switches to it.
Your new work can now be committed separately from main.
3. Make changes and commit them
After modifying your files:
git status
git add .
git commit -m "Add login functionality"
The commit is recorded on the current branch.
4. Switch between branches
To return to the main branch:
git switch main
To return to your feature branch:
git switch feature/login
Git switches the working tree to the selected branch, provided there are no conflicting uncommitted changes.
5. Merge your feature branch
When the feature is ready, you can merge it into the main branch.
First:
git switch main
Then:
git merge feature/login
Git attempts to combine the changes.
If the same parts of files were changed differently, a merge conflict may occur.
6. Understand merge conflicts
A merge conflict happens when Git cannot automatically determine how to combine changes.
For example:
<<<<<<< HEAD
Welcome to the application.
=======
Welcome to our software platform.
>>>>>>> feature/login
You must review the conflicting section and choose the correct final content.
After resolving the conflict:
git add .
git commit
The exact workflow may differ depending on whether Git created a merge commit or another merge state.
7. Push your branch to GitHub
To upload your feature branch:
git push -u origin feature/login
Other collaborators can now review it.
A common team workflow is to create a pull request so changes can be reviewed before merging.
8. Delete a completed branch
After merging, a local feature branch can be deleted:
git branch -d feature/login
To delete a remote branch:
git push origin --delete feature/login
Only delete branches when you are confident their work has been integrated or is no longer needed.
A practical branching workflow
1. Update main
2. Create a feature branch
3. Implement the feature
4. Commit changes
5. Push the branch
6. Open a pull request
7. Review and test
8. Merge into main
Why branching matters
Git branching helps developers:
- Work on multiple features independently.
- Experiment without immediately affecting main.
- Review changes before integration.
- Organize bug fixes.
- Collaborate more effectively.
For beginners, learning branching early makes working with real-world repositories much easier.
Final thoughts
Git is more than a way to upload source code.
It is a development workflow that helps teams manage changes and collaborate safely.
Start with simple feature branches, practice merging, and learn how to resolve conflicts.
Question: Do you normally use Git branches for every feature, or do you work directly on the main branch?
Top comments (0)