Git Branch Management
Branching is one of Git's most powerful features. It allows you to develop different features in parallel and experiment with new ideas without affecting the main code. This chapter will detail the creation, management, and usage of Git branches.
Basic Branch Concepts
What is a Branch?
A branch is essentially a movable pointer to a commit. Git's default branch is usually called main or master.
Branch Diagram:
A---B---C---D (main branch)
\
E---F (feature branch)Advantages of Branches
- 🔄 Parallel Development: Multiple features can be developed simultaneously.
- 🧪 Safe Experimentation: Try new ideas on a branch without affecting main code.
- 👥 Team Collaboration: Everyone can work on their own branch.
- 🚀 Fast Switching: Branch creation and switching are nearly instantaneous.
Viewing Branches
Basic Viewing Commands
bash
# View local branches
git branch
# View all branches (including remote)
git branch -a
# View remote branches
git branch -r
# View detailed branch info
git branch -v
# View branch tracking relationships
git branch -vvPractical Example
bash
# Create demo project
mkdir git-branch-demo
cd git-branch-demo
git init
# Create initial commit
echo "# Branch Management Demo" > README.md
git add README.md
git commit -m "Initial commit"
# View current branch
git branchOutput:
* mainThe asterisk * indicates the current branch.
Creating Branches
Methods to Create Branches
bash
# Create new branch (but don't switch)
git branch feature-login
# Create and switch to new branch
git checkout -b feature-register
# Create and switch using new syntax (Git 2.23+)
git switch -c feature-profile
# Create branch from specific commit
git branch hotfix-bug commit_hash
# Create local branch from remote branch
git checkout -b local-branch origin/remote-branchBranch Naming Conventions
bash
# ✅ Good branch names
git branch feature/user-authentication
git branch bugfix/login-error
git branch hotfix/security-patch
git branch release/v1.2.0
git branch experiment/new-algorithm
# ❌ Avoid these names
git branch test
git branch temp
git branch branch1
git branch fix-bug # Use English and avoid special characters/spacesBranch Creation Example
bash
# Create feature branches
git branch feature/user-login
git branch feature/user-register
git branch feature/password-reset
# View all branches
git branchOutput:
feature/password-reset
feature/user-login
feature/user-register
* mainSwitching Branches
Methods to Switch Branches
bash
# Switch to existing branch
git checkout feature/user-login
# Switch using new syntax (Git 2.23+)
git switch feature/user-login
# Switch to previous branch
git checkout -
git switch -
# Force switch (discard working directory changes) ⚠️ Dangerous
git checkout -f branch-nameNotes on Switching Branches
bash
# Create changes to demonstrate
echo "New feature code" > login.py
echo "Config file" > config.json
# Check status
git status
# Try to switch branch
git checkout feature/user-loginIf there are uncommitted changes, Git will prevent switching:
error: Your local changes to the following files would be overwritten by checkout:
login.py
Please commit your changes or stash them before you switch branches.Solutions:
bash
# Method 1: Commit changes
git add .
git commit -m "WIP: Developing login feature"
git checkout feature/user-login
# Method 2: Stash changes
git stash
git checkout feature/user-login
# Restore later
git stash pop
# Method 3: Force switch (Lose changes)
git checkout -f feature/user-loginMerging Branches
Fast-forward Merge
When the target branch has no new commits, Git performs a fast-forward merge:
bash
# Switch to feature branch
git checkout feature/user-login
echo "Login feature implementation" > login.py
git add login.py
git commit -m "Implement user login"
# Switch back to main branch
git checkout main
# (Merge command would follow)