Create Branch
Learn how to create Git branches with detailed step-by-step instructions, practical examples, and best practices for different scenarios.
Creating Git Branches
Creating a branch in Git is like creating a new workspace where you can experiment and develop features without affecting the main codebase.
Basic Branch Creation
Method 1: Create and Stay on Current Branch
git branch branch_name
This command creates a new branch called branch_name
based on your current branch, but keeps you on your current branch.
Method 2: Create and Switch (Recommended)
git checkout -b branch_name
This command creates a new branch called branch_name
and immediately switches to it. This is the most commonly used method.
Method 3: Modern Git Syntax (Git 2.23+)
git switch -c branch_name
This is the newer, more intuitive way to create and switch to a new branch.
Step-by-Step Guide: Creating Your First Branch
Scenario: Adding a Contact Form to a Website
Let's walk through creating a branch for adding a contact form to your website.
Step 1: Check your current status
git status
Expected Output:
On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree clean
Step 2: Make sure you're on the main branch
git checkout main
Expected Output:
Already on 'main'
Step 3: Get the latest changes
git pull origin main
Expected Output:
Already up to date.
Step 4: Create and switch to your new branch
git checkout -b feature/contact-form
Expected Output:
Switched to a new branch 'feature/contact-form'
Step 5: Verify you're on the new branch
git branch
Expected Output:
* feature/contact-form
main
The *
indicates your current branch.
Different Branch Creation Scenarios
1. Feature Development Branch
When to use: Adding new functionality
# Creating a branch for user authentication
git checkout main
git pull origin main
git checkout -b feature/user-authentication
# Creating a branch for search functionality
git checkout -b feature/search-system
# Creating a branch for payment integration
git checkout -b feature/payment-gateway
Visual Representation:
Before:
main: A---B---C (HEAD)
After creating feature/user-authentication:
main: A---B---C
↘
feature/user-authentication: A---B---C (HEAD)
2. Bug Fix Branch
When to use: Fixing non-critical bugs
# Fix a bug in the navigation menu
git checkout -b bugfix/navigation-menu-error
# Fix a styling issue
git checkout -b bugfix/button-alignment-issue
3. Hotfix Branch
When to use: Fixing critical issues in production
# Critical security fix
git checkout main
git checkout -b hotfix/security-vulnerability
# Critical payment system fix
git checkout -b hotfix/payment-system-crash
4. Experimental Branch
When to use: Trying out new ideas
# Experimenting with a new design
git checkout -b experiment/new-ui-design
# Testing a new library
git checkout -b experiment/react-query-integration
Branch Naming Conventions
Good Naming Patterns
Type | Pattern | Example |
---|---|---|
Feature | feature/description | feature/user-profile |
Bug Fix | bugfix/description | bugfix/login-error |
Hotfix | hotfix/description | hotfix/payment-crash |
Experiment | experiment/description | experiment/new-framework |
Documentation | docs/description | docs/api-documentation |
Naming Best Practices
✅ Good Examples:
feature/shopping-cart
bugfix/header-navigation
hotfix/security-patch
docs/installation-guide
❌ Bad Examples:
my-branch
(too vague)temp
(unclear purpose)new-stuff
(not descriptive)branch-1
(meaningless)
Creating Branches from Specific Points
Create Branch from Another Branch
# Create a feature branch from develop branch
git checkout develop
git checkout -b feature/new-feature
# Or in one command:
git checkout -b feature/new-feature develop
Create Branch from a Specific Commit
# Create branch from a specific commit hash
git checkout -b hotfix/old-bug a1b2c3d4
# Create branch from a tag
git checkout -b feature/from-v1.0 v1.0.0
Visual Example:
Commit History:
main: A---B---C---D---E (HEAD)
↑
└─ Create branch here
Command: git checkout -b bugfix/issue-from-B a1b2c3d4
Result:
main: A---B---C---D---E
↘
bugfix/issue-from-B: A---B (HEAD)
Practical Exercise: Creating Multiple Branches
Let's practice with a real scenario. Imagine you're working on a blog website:
1. Create a branch for adding comments:
git checkout main
git checkout -b feature/add-comments
# Work on comments feature
2. Switch back and create a branch for fixing header:
git checkout main
git checkout -b bugfix/fix-header-responsive
# Work on header fix
3. Create a branch for experimenting with dark mode:
git checkout main
git checkout -b experiment/dark-mode-theme
# Experiment with dark mode
Visual Result:
Repository Structure:
main: A---B---C
↘ ↘ ↘
feature/add-comments: A---B---C
bugfix/fix-header-responsive: A---B---C
experiment/dark-mode-theme: A---B---C
Common Scenarios and Solutions
Scenario 1: "I forgot to create a branch and made changes on main"
Problem: You've been working on main and have uncommitted changes.
Solution:
# 1. Create and switch to new branch (changes come with you)
git checkout -b feature/my-work
# 2. Commit your changes
git add .
git commit -m "Add my feature work"
# 3. Switch back to main (which is now clean)
git checkout main
Scenario 2: "I need to create a branch but have uncommitted changes"
Problem: You have changes but want to start a new branch.
Solution Option 1 - Stash changes:
# 1. Stash your current changes
git stash
# 2. Create new branch
git checkout -b feature/new-work
# 3. Apply stashed changes if needed
git stash pop
Solution Option 2 - Commit then branch:
# 1. Commit your current work
git add .
git commit -m "Work in progress"
# 2. Create new branch
git checkout -b feature/continue-work
Scenario 3: "I want to create a branch with the same name that exists"
Problem: Branch name already exists.
Error Message:
fatal: A branch named 'feature/login' already exists.
Solutions:
# Option 1: Use a different name
git checkout -b feature/login-v2
# Option 2: Delete old branch first (if safe)
git branch -d feature/login
git checkout -b feature/login
# Option 3: Switch to existing branch
git checkout feature/login
Verifying Your Branch Creation
After creating a branch, always verify:
1. Check current branch:
git branch
2. Check branch with commit info:
git branch -v
Expected Output:
* feature/contact-form a1b2c3d Add contact form structure
main e4f5g6h Latest main commit
3. Check remote tracking:
git branch -vv
Next Steps After Creating a Branch
- Start working: Make your changes and commits
- Push to remote:
git push origin branch-name
- Create pull request: For code review and merging
- Keep updated: Regularly sync with main branch