Git Notes
Learn how to create branches in Git for feature development, bug fixes, and experimentation. Understand branch creation from different starting points.
Creating branches is one of Git's most powerful features. Branches let you work on features, fixes, or experiments in isolation without affecting the main codebase.
Understanding Branches
A branch in Git is simply a lightweight pointer to a specific commit. When you create a branch, Git creates a new pointer — it does not copy any files.
Before creating branch
main → [C1] ← [C2] ← [C3] (HEAD)
After creating feature branch
main → [C1] ← [C2] ← [C3]
↑
feature ──────────────────────┘ (HEAD)
Creating Branches
Method 1: git branch (Create Only)
This creates the branch but does not switch to it:
git branch feature-login(no output - branch created silently)
You remain on your current branch. To switch:
git checkout feature-login
# or
git switch feature-loginMethod 2: git checkout -b (Create and Switch)
The most common approach — creates and switches in one command:
git checkout -b feature-loginSwitched to a new branch 'feature-login'
Method 3: git switch -c (Modern Approach)
The newer, more intuitive command:
git switch -c feature-loginSwitched to a new branch 'feature-login'
Creating Branches from Different Points
From Current Branch (Default)
# Creates branch at current HEAD
git checkout -b new-featureFrom a Specific Branch
# Create branch from develop, not from current branch
git checkout -b feature-api developFrom a Specific Commit
# Create branch from a past commit
git checkout -b hotfix-v1 abc1234Switched to a new branch 'hotfix-v1'
From a Tag
# Create branch from a release tag
git checkout -b hotfix-release v2.0.0From a Remote Branch
# Create local branch tracking a remote branch
git checkout -b feature-x origin/feature-x
# Shorter form (auto-tracks if remote branch exists)
git checkout feature-xBranch 'feature-x' set up to track remote branch 'feature-x' from 'origin'. Switched to a new branch 'feature-x'
Step-by-Step Tutorial
Setting Up a Feature Branch Workflow
# Start on main branch with latest code
git checkout main
git pull origin main
# Create your feature branch
git checkout -b feature/user-dashboard
# Verify you are on the new branch
git branchmain * feature/user-dashboard
# Start working on your feature
echo "Dashboard component" > dashboard.js
git add dashboard.js
git commit -m "Add initial dashboard component"
# Continue working...
echo "Dashboard styles" > dashboard.css
git add dashboard.css
git commit -m "Add dashboard styles"
# Check your branch history
git log --onelineb3c4d5e (HEAD -> feature/user-dashboard) Add dashboard styles a1b2c3d Add initial dashboard component f7e8d9c (origin/main, main) Initial commit
Listing Branches
# List local branches
git branchdevelop main * feature/user-dashboard feature/api-integration
# List remote branches
git branch -rorigin/main origin/develop origin/feature/notifications
# List all branches (local + remote)
git branch -adevelop main * feature/user-dashboard remotes/origin/main remotes/origin/develop remotes/origin/feature/notifications
# List branches with last commit info
git branch -vdevelop a1b2c3d Add API endpoint main f7e8d9c Initial commit * feature/user-dashboard b3c4d5e Add dashboard styles
Branch Creation Best Practices
Always Start from an Updated Base
# Update main before branching
git checkout main
git pull origin main
git checkout -b feature/new-featureUse Descriptive Names
# Good branch names
git checkout -b feature/JIRA-123-user-authentication
git checkout -b bugfix/fix-login-redirect
git checkout -b hotfix/patch-xss-vulnerability
# Bad branch names
git checkout -b my-branch
git checkout -b test
git checkout -b fixKeep Branches Focused
Each branch should represent one logical change:
# Good: One feature per branch
git checkout -b feature/add-search
git checkout -b feature/add-filters
# Bad: Multiple unrelated changes in one branch
git checkout -b feature/search-and-filters-and-refactorCommon Errors and Solutions
Branch Already Exists
git checkout -b feature-loginfatal: a branch named 'feature-login' already exists
Solution: Use a different name or switch to the existing branch:
git checkout feature-login # Switch to existing
# or
git checkout -b feature-login-v2 # Create with new nameUncommitted Changes Block Branch Switch
git checkout mainerror: Your local changes to the following files would be overwritten by checkout:
app.js
Please commit your changes or stash them before you switch branches.Solution: Commit, stash, or discard changes:
# Option 1: Commit changes
git add . && git commit -m "WIP: save progress"
# Option 2: Stash changes
git stash
git checkout main
git checkout -b new-branch
git stash pop
# Option 3: Discard changes (careful!)
git checkout -- .Interview Questions
- What is the difference between
git branch featureandgit checkout -b feature?
git branch feature creates the branch but keeps you on the current branch. git checkout -b feature creates the branch and immediately switches to it.
- How do you create a branch from a specific commit?
Use git checkout -b branch-name commit-hash. This creates a new branch pointing to that specific commit in history.
- What happens internally when you create a branch?
Git creates a new pointer (a 41-byte file containing the commit SHA) in .git/refs/heads/. No files are copied — branches are extremely lightweight.
- How do you create a local branch that tracks a remote branch?
Use git checkout -b local-name origin/remote-name or simply git checkout remote-name if a matching remote branch exists — Git auto-creates a tracking branch.
- Should you create a branch from main or develop?
It depends on your branching strategy. In GitHub Flow, branch from main. In GitFlow, feature branches come from develop, while hotfixes come from main.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Create Branch in Git - Start New Development Lines.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Git & GitHub topic.
Search Terms
git-github, git & github, git, github, branching, and, merging, create
Related Git & GitHub Topics