Branching is one of Git's most powerful features.
Branches allow developers to isolate work, experiment safely, and collaborate without interrupting the main codebase.
What Is a Branch?
A branch is a lightweight pointer to a specific commit.
It enables safe experimentation and parallel development.
Common branch examples:
- main
- develop
- feature/homepage
- bugfix/login
Creating a New Branch
Create a branch named feature/homepage:
git branch feature/homepageCreate AND switch to it immediately:
git checkout -b feature/homepageEquivalent commands:
git branch feature/homepage
git checkout feature/homepageListing Branches
Show all local branches:
git branchShow local and remote branches:
git branch -aExample output:
* main
feature/homepageSwitching Between Branches
Switch to another branch:
git checkout feature/homepageSwitch back to main:
git checkout mainModern Git commands:
git switch feature/homepage
git switch mainMerging Branches
First switch to the branch you want to merge INTO:
git checkout mainThen merge your feature branch:
git merge feature/homepageGit will attempt an automatic merge (fast-forward or merge commit).
Handling Merge Conflicts
If two branches edit the same lines of code, Git will produce a conflict.
A conflict looks like this:
<<<<<<< HEAD
your current changes
======
incoming changes from merged branch
>>>>>>> feature/homepageResolve conflicts by:
- Editing the file
- Choosing the correct content
- Removing conflict markers
- Staging the file:
git add filename- Completing the merge:
git commit