Branch
Introduction: Why Branching Matters π³β
Imagine working on a big project with your team. Someone is fixing bugs while others are building new features. How do you all work together without stepping on each other's toes? Git branches are the answer!
What is a Branch? πΏβ
A branch is a parallel version of your code where you can work independently without affecting the main codebase.
Real-World Analogy: Writing a Book πβ
Main Story (main branch)
Chapter 1 β
Chapter 2 β
Chapter 3 β
β
ββββββ΄βββββββββββββββββββββ
β β
Experimental Alternative
Ending Branch Subplot Branch
β β
β Try different β Add new
β conclusion β character
β β
β Like it? Merge! β Didn't work? Delete!
β β
Main Story
Chapter 4 β (with new ending)
Key Concept:
- Main branch = The official, working version
- Feature branches = Experimental playgrounds
- Merge = Bring changes back when ready
- Delete = Discard failed experiments
Visual: How Branching Worksβ
main βββββββββββββββββββββββ
β β
feature βββββββββ (created, worked on, merged back)
Timeline:
- Start from main branch (commit 2)
- Create feature branch
- Work on feature independently
- Merge back to main when ready
- Main branch now has your feature!
Why Use Branches? π‘β
1. Safe Experimentation π§ͺβ
Try new ideas without breaking working code!
main βββββββββββ (stable, always works)
β
experiment βββββ (test crazy idea safely)
2. Parallel Development π₯β
Multiple features at once:
main ββββββββββββββββββββ
β β
feature-login βββββββββ (Team A works here)
β β
feature-payments βββββ (Team B works here)
3. Organized Workflow πβ
Keep different types of work separated:
main Production-ready code
β
develop Integration branch
β
feature/* New features
β
hotfix/* Urgent bug fixes
4. Easy Rollback βͺβ
Mistakes happen. Delete bad branches!
main βββββββββββ (safe and sound)
β
bad-idea βββββ β Delete this, no harm done!
Real-World Benefitsβ
β Isolation - Work on features without affecting others β Collaboration - Multiple developers work simultaneously β Experimentation - Try ideas risk-free β Organization - Separate bugs, features, hotfixes β Safety - Main branch stays stable β Rollback - Abandon bad ideas easily
Creating and Switching Branches πβ
Method 1: Create and Switch (Recommended) ββ
Modern way (Git 2.23+):
git switch -c feature-login
Traditional way (works everywhere):
git checkout -b feature-login
What happens:
Before:
main βββββββββββ β You are here
β
After:
main βββββββββββ
β
feature-login β β You are now here (new branch)
Both commands:
- Create a new branch called
feature-login - Switch to it immediately
- Start from current commit
Method 2: Create Then Switch (Two Steps)β
Step 1: Create the branch
git branch feature-login
Step 2: Switch to it
git switch feature-login # Modern way
# or
git checkout feature-login # Traditional way
Visual:
Step 1:
main βββββββββββ β You are still here
β
feature β (branch created but you haven't moved)
Step 2:
main βββββββββββ
β
feature β β Now you moved here
Quick Reference: Branch Commandsβ
| Task | Modern Command | Traditional Command |
|---|---|---|
| Create & switch | git switch -c name | git checkout -b name |
| Switch only | git switch name | git checkout name |
| Create only | git branch name | git branch name |
| List branches | git branch | git branch |
Modern (git switch):
- β Clear and intuitive
- β Less error-prone
- β Available Git 2.23+
Traditional (git checkout):
- β Works everywhere
- β οΈ Does many things (confusing)
- β οΈ Easy to make mistakes
Recommendation: Use git switch if available!
Example Workflow: Creating a Login Featureβ
# 1. Make sure you're on main branch
git switch main
# 2. Get latest changes
git pull origin main
# 3. Create new feature branch
git switch -c feature-login
# 4. Now you can work safely!
# Make changes to files...
# 5. Check what branch you're on
git branch
# Output:
# main
# * feature-login β The * shows current branch
Visual Timeline:
main βββββββββββ (pulled latest)
β
βββ β Created feature-login, started working
Merging Branches πβ
Merging brings changes from one branch into another.
Basic Merge Workflowβ
Step 1: Switch to destination branch
git switch main # Go to branch receiving changes
Step 2: Merge the feature branch
git merge feature-login
Visual:
Before merge:
main βββββββββββ
β
feature-login βββββββββ (3 new commits)
After merge:
main βββββββββββββββββ (merged all changes)
β
feature-login βββββββββ (branch still exists)
Complete Example: Feature Developmentβ
# You're on main branch
git switch main
# Get latest changes
git pull origin main
# Create feature branch
git switch -c feature-user-profile
# Work on feature
# ... make changes, commit ...
git add .
git commit -m "Add user profile page"
# More work...
git commit -m "Add profile edit functionality"
# Feature complete! Switch back to main
git switch main
# Merge feature into main
git merge feature-user-profile
# Push to remote
git push origin main
Timeline:
main ββββββββββββββββββββ (merged feature)
β β
feature βββββββββββββ (2 commits)
Merge Conflicts (When Things Collide) β οΈβ
What is a conflict? When two branches modify the same part of a file differently.
Example scenario:
main Modified line 10 in app.js
β
β
β
feature-login Also modified line 10 in app.js
β
β Conflict! Git doesn't know which version to keep.
How Git shows conflicts:
// app.js
<<<<<<< HEAD (current branch)
const title = "Welcome to Dashboard";
=======
const title = "User Login Page";
>>>>>>> feature-login (incoming branch)
Resolving:
- Open the file
- Choose which version to keep (or combine them)
- Remove conflict markers (
<<<<<<<,=======,>>>>>>>) - Save the file
- Stage and commit
# After manually fixing conflicts
git add app.js
git commit -m "Resolve merge conflict in app.js"
If main hasn't changed since you branched, Git does a fast-forward merge:
Before:
main ββββββ
β
feature βββββββββ
After (fast-forward):
main ββββββββββββββ β Just moves pointer forward!
No merge commit neededβcleaner history! β¨
Deleting Branches ποΈβ
Once merged, feature branches can be deleted to keep things tidy.
Safe Delete (Merged Only)β
git branch -d feature-login
What it does:
- β Deletes branch if already merged
- β Refuses if not merged (safety!)
Visual:
Before:
main βββββββββββββββββ (feature merged)
β
feature-login βββββββββ β Delete this
After:
main βββββββββββββββββ (commits remain)
β
(branch deleted)
Force Delete (Caution!) β οΈβ
git branch -D feature-experiment
What it does:
- π¨ Deletes branch even if NOT merged
- β οΈ DANGER: Commits may be lost!
Use when:
- Abandoning failed experiments
- Removing mistake branches
- You're absolutely sure!
# β DON'T delete the current branch
git switch feature-login
git branch -d feature-login # ERROR!
# β
DO switch away first
git switch main
git branch -d feature-login # SUCCESS!
Delete Remote Branchβ
# Delete local branch
git branch -d feature-login
# Delete from GitHub/remote
git push origin --delete feature-login
Visual:
Local: feature-login deleted β
β
Remote: feature-login deleted β (after push --delete)
Renaming Branches πβ
Rename Current Branchβ
git branch -m new-name
Example:
# You're on branch: feature-login-page
git branch -m feature-auth-system
# Now branch is called: feature-auth-system
Rename Other Branchβ
git branch -m old-name new-name
Example:
git branch -m bugfix-typo bugfix-spelling-errors
Visual:
Before:
main ββββββ
β
feature-login-page βββββ
After rename:
main ββββββ
β
feature-auth-system βββββ (same commits, new name!)
Good reasons:
- Scope changed during development
- Found better, clearer name
- Standardize naming convention
- Fix typos
Remember: Branch names are just labels. Renaming is safe! π·οΈ
Local vs Remote Branches πβ
Two copies of your work exist:
ββββββββββββββββββββββββββββββββββββββββββ
β Your Computer (Local) β
β ββββββββββββββββ β
β β main β β Local branch β
β β feature-auth β β
β ββββββββββββββββ β
ββββββββββββββββββββββββββββββββββββββββββ
β (push/pull)
ββββββββββββββββββββββββββββββββββββββββββ
β GitHub (Remote) β
β ββββββββββββββββ β
β β origin/main β β Remote branch β
β ββββββββββββββββ β
ββββββββββββββββββββββββββββββββββββββββββ
Understanding the Namesβ
| Name | Meaning | Location |
|---|---|---|
main | Your local main branch | Your computer |
origin/main | Remote main branch | GitHub |
feature-login | Your local feature | Your computer |
origin/feature-login | Remote feature | GitHub |
Key Point: origin = default name for remote repository (usually GitHub)
Pushing to Remote (Share Your Work) β¬οΈβ
First time pushing a new branch:
git push -u origin feature-login
What -u does:
- Sets up tracking between local and remote branch
- Future pushes can just use
git push - Creates connection for easy sync
Visual:
Before push:
Local: main ββββββ
β
feature βββββββββ β Only on your computer
Remote: main ββββββ β Team can't see your work
After push:
Local: feature βββββββββ
β (tracking)
Remote: origin/feature βββββββββ β Now team can see!
Subsequent pushes:
git push # Simple! (tracking already set up)
Pulling from Remote (Get Others' Work) β¬οΈβ
git pull origin main
What happens:
- Fetch latest changes from GitHub
- Merge them into your current branch
Visual:
Remote: main ββββββββββββββββ (teammates pushed new commits)
β
Local: main ββββββ β Your branch is behind
After pull:
Local: main ββββββββββββββββ β Now up to date!
Fetch vs Pull: What's the Difference? π€β
This confuses many beginners! Let's make it crystal clear.
The Key Differenceβ
git fetch = Download + Review (safe, no changes to your code)
git pull = Download + Merge (automatic, changes your code)
Git Fetch (Look Before You Leap) πβ
Downloads changes but doesn't apply them.
git fetch origin
The command goes out to that remote project and pulls down all the data from that remote project that you don't have yet. After you do this, you should have references to all the branches from that remote, which you can merge in or inspect at any time.
If you clone a repository, the command automatically adds that remote repository under the name origin. So, git fetch origin fetches any new work that has been pushed to that server since you cloned (or last fetched from) it.
git fetch command only downloads the data to your local repositoryβββit doesn't automatically merge it with any of your work or modify what you're currently working on. You have to merge it manually into your work when you're ready.
You can also fetch a specific branch:
git fetch origin new-branch
This will fetch the new-branch branch from the remote repository.
Once you have fetched a branch, and you want to merge it into your current branch, you can use the git merge command:
git merge origin/new-branch
This will merge the new-branch branch into the current branch.
Pulling branchesβ
To pull branches, you can use the git pull command. This command takes one argument, which is the name of the remote repository.
git pull origin
This will pull all branches from the remote repository. You can also pull a specific branch using the -b flag:
git pull origin new-branch
This will pull the new-branch branch from the remote repository.
git pull command to automatically fetch and then merge that remote branch into your current branch. This may be an easier or more comfortable workflow for you; and by default, the git clone command automatically sets up your local master branch to track the remote master branch (or whatever the default branch is called) on the server you cloned from. Running git pull generally fetches data from the server you originally cloned from and automatically tries to merge it into the code you're currently working on.
git fetch vs git pullβ
git fetchβ
The git fetch command will fetch all branches from a remote repository. But it will not merge these branches into your local branches.
To understand it better, let's imagine that you have a local branch called main and a remote branch called main. If you run git fetch origin, it will fetch the main branch from the remote repository. However, it will not merge this branch into your local main branch. Your local branch won't have the latest changes that remote branch have, unless You merge this branch manually by git merge origin/main
Merging branch means that you are merging the changes from the remote branch into your local branch. This means that your local branch will be updated with the changes from the remote branch.
git pullβ
The git pull command will fetch all branches from a remote repository and merge them into your local branches. This means that your local branches will be updated with the changes from the remote branches.
So, git pull origin main = git fetch origin/main + git merge origin/main
origin/branch-name vs branch-nameβ
origin/branch-name is the remote branch
branch-name is the local branch
Both branches can have different commits. When we run git fetch, it will fetch the latest commits from the remote repository, and put then in origin/branch-name. But it will not update the local branch.
Popular Branching Workflows π³β
Different teams use different branching strategies. Here are the most popular:
Quick Comparisonβ
| Workflow | Best For | Complexity | Release Frequency |
|---|---|---|---|
| GitHub Flow | Small teams, continuous deployment | β Simple | Very frequent |
| Git Flow | Scheduled releases, larger teams | βββ Complex | Periodic releases |
| Trunk-Based | Advanced teams, CI/CD | ββ Medium | Continuous |
| GitLab Flow | Production + staging envs | ββ Medium | Frequent |
| Feature Branch | Any team size | β Simple | Flexible |
Learn Moreβ
- Gitflow - Structured workflow with release branches
- GitHub Flow - Simple, deploy from main
- GitLab Flow - Environment-based branching
- Trunk-Based Development - Short-lived branches
- Feature Branch Workflow - One branch per feature
Start simple: If you're new, use GitHub Flow or Feature Branch Workflow.
Need structure: Use Git Flow for scheduled releases.
Advanced CI/CD: Consider Trunk-Based Development.
No perfect choice: Workflows can be adapted! Pick one and adjust as needed.
Branch Naming Conventions πβ
Good branch names help teams understand what everyone is working on at a glance!
Why Naming Mattersβ
β Bad: stuff, test, asdf, new-branch
β
Good: feature/user-login, bugfix/cart-crash, hotfix/security-patch
Benefits of good names:
- Team knows what you're working on
- Easy to find specific branches
- Clear project organization
- Better collaboration
The 6 Golden Rulesβ
Follow these guidelines for professional branch naming:
1. Short and Descriptive βοΈβ
Balance brevity with clarity.
β Too vague: fix, update, new
β
Just right: bugfix/login-error, feature/dark-mode
β Too long: feature/add-new-user-authentication-system-with-oauth2
Examples:
feature/login-formβbugfix/cart-crashβhotfix/security-patchβ
2. Use Lowercase π‘β
Easier to type and read.
β Wrong: Feature/UserLogin, BugFix/cart-crash
β
Right: feature/user-login, bugfix/cart-crash
3. Hyphen-Separated (kebab-case) πβ
Use hyphens instead of spaces or underscores.
β Wrong: feature/userlogin, feature/user_login, feature/userLogin
β
Right: feature/user-login
More examples:
prepare-release-v2βissue-426-fixβui-redesignβ
4. Prefix with Type π·οΈβ
Quickly identify branch purpose!
Common Prefixes:
| Prefix | Purpose | Example |
|---|---|---|
main / master | Production code | main |
develop | Development integration | develop |
feature/ | New features | feature/shopping-cart |
bugfix/ | Bug fixes | bugfix/login-error |
hotfix/ | Urgent production fixes | hotfix/security-issue |
release/ | Release preparation | release/v2.1.0 |
wip/ | Work in progress | wip/experimental-ui |
Real Examples:
feature/user-authentication
bugfix/payment-validation
hotfix/critical-crash
release/v1.5.0
wip/new-algorithm
Combining Prefixes:
feature/issue-123/user-profile
hotfix/bug-456/memory-leak
List branches by type:
git branch --list 'feature/*'
Output:
feature/login
feature/cart
feature/checkout
5. Be Consistent π―β
Team-wide standards matter!
β
Team uses feature/ β Everyone uses feature/
β
Team skips prefixes β Nobody uses prefixes
β Mixed approach β Confusion!
Pick ONE convention and stick to it!
6. Don't Fear Renaming πβ
Branch names are just labelsβrenaming is safe!
Rename current branch:
git branch -m better-name
Rename any branch:
git branch -m old-name new-name
When to rename:
- Found a clearer name
- Feature scope changed
- Fixed a typo
- Standardizing conventions
Key Takeaways π―β
Branching empowers you to: β Experiment safely without breaking working code β Work on multiple features simultaneously β Collaborate with teams without conflicts β Organize different types of work (features, bugs, hotfixes) β Keep production code stable
Remember:
- Create branches for each feature/fix
- Use clear, descriptive names
- Merge when ready, delete when done
- Fetch to review, pull to update
- Local and remote branches are separate (until you push!)
Master these commands:
git switch -c feature-name # Create and switch
git merge feature-name # Merge changes
git branch -d feature-name # Delete merged branch
git fetch origin # Download changes (safe)
git pull origin main # Download + merge
You're now ready to:
- Create feature branches
- Merge changes safely
- Collaborate with teams
- Use professional naming conventions
- Choose a branching workflow
Keep practicing, and branching will become second nature! π