Git Branch Management and Parallel Development
Branches are one of Git’s most powerful features. With branches, teams can develop different features in parallel without affecting each other, and then merge them together later. Creating branches in Git is extremely easy, which encourages frequent use of branches.
1. Basic Concepts of Branches
(1) What Is a Branch?
In Git, a branch is a movable pointer that points to a specific commit. Each time a commit is made, the current branch pointer automatically moves forward to point to the new commit.
graph LR
C1[SubmitC1] --> C2[SubmitC2] --> C3[SubmitC3]
main[mainBranch] --> C3
HEAD[HEAD] --> main
style main fill:#d4edda
style HEAD fill:#fff3cd
(2) The Nature of Branches
A Git branch is essentially a file containing 41 bytes:
- A 40-byte SHA-1 hash value
- A 1-byte line feed character
That's why Git creates branches so quickly.
# View Branch Files
cat .git/refs/heads/main
# Output:
# a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0
(3) HEAD Pointer
HEAD is a special pointer that points to the current branch:
graph TB
HEAD[HEAD] --> main[mainBranch]
main --> C3[SubmitC3]
C3 --> C2[SubmitC2]
C2 --> C1[SubmitC1]
style HEAD fill:#f8d7da
style main fill:#d4edda
(4) The Advantages of Branches
- Parallel Development: Multiple people working simultaneously on different branches
- Functional Isolation: New feature development does not affect the main branch
- Safe Experimentation: Experiment on a branch; if it fails, delete it
- Version Control: Different versions are maintained on different branches
2. View Branches
(1) View local branches
▶ Example: List branches
# View local branches
git branch
# Output:
# * main
# develop
# feature
# * Indicates the current branch
# View Branches and Last Commit
git branch -v
# Output:
# * main a1b2c3d feat: Add a login feature
# develop d4e5f6g Fix: FixBug
# feature h7i8j9k WIP: New Features
# View detailed branch information
git branch -vv
# The output includes information about the upstream branch:
# * main a1b2c3d [origin/main] feat: Add a login feature
(2) View all branches
▶ Example: Viewing a remote branch
# View All Branches(Local+Remote)
git branch -a
# Output:
# * main
# develop
# remotes/origin/HEAD -> origin/main
# remotes/origin/main
# remotes/origin/develop
# remotes/origin/feature
# View only remote branches
git branch -r
# Output:
# origin/HEAD -> origin/main
# origin/main
# origin/develop
# origin/feature
(3) View Branch Relationships
▶ Example: Graphical Display of Branches
# View the branch history graph
git log --oneline --graph --all
# Output:
# * a1b2c3d (HEAD -> main) feat: Add Login
# | * d4e5f6g (feature) WIP: New Features
# |/
# * h7i8j9k Initial commit
# View the commits included in a branch
git log --oneline --graph --decorate --all
# View Branch Fork Points
git merge-base main feature
3. Create a Branch
(1) Create a new branch
▶ Example: Creating a Branch
# Create a Branch(Based on the current commit)
git branch feature
# Create a branch and specify its starting point
git branch feature a1b2c3d
# Create a branch based on a specific tag
git branch v1.1-branch v1.0.0
# Create a branch and switch to it immediately
git checkout -b feature
# New Grammar(Recommendations)
git switch -c feature
# Created from a remote branch
git checkout -b feature origin/feature
# Or
git switch -c feature origin/feature
(2) Workflow for Creating a Branch
graph TB
A[mainBranch] --> B[git branch feature]
B --> C[New Branchfeature]
C --> D[feature: Go to Development]
D --> E[Submit Changes]
E --> F[Merge back intomain]
style A fill:#d4edda
style C fill:#fff3cd
style F fill:#c3e6cb
(3) Branch Naming Conventions
▶ Example: Standard Branch Naming Conventions
Feature Branch:feature/user-auth
feature/shopping-cart
feature/payment-integration
Fix Branch:fix/login-error
fix/memory-leak
Thermal Repair Branch:hotfix/critical-security-issue
hotfix/payment-failure
Release Branch:release/v1.0.0
release/v2.1.0
Development Branch:develop
Main Branch: main (or master)
4. Switch Branches
(1) Commands for switching branches
▶ Example: Switching Branches
# Traditional Commands
git checkout feature
# New Command(Recommendations)
git switch feature
# Switch to the previous branch
git switch -
# Switch tomainBranch
git switch main
# Create and Switch
git switch -c new-feature
# Output:
# Switched to branch 'new-feature'
(2) How Branch Switching Works
graph TB
A[Before switching branches] --> B[UpdateHEADPointer]
B --> C[Update the temporary storage area]
C --> D[Update Workspace]
D --> E[Switching complete]
style A fill:#fff3cd
style E fill:#d4edda
When switching branches, Git will:
- Update the HEAD to point to the new branch
- Update the staging area to reflect the status of the new branch
- Update the files in the workspace with the contents of the new branch
(3) Handling Unsaved Changes
▶ Example: Processing Changes Before Switching Branches
# View Current Status
git status
# Method1:Submit Changes
git add .
git commit -m "WIP: Save as Draft"
git switch feature
# Method2:Save Changes Temporarily(stash)
git stash
git switch feature
# After completing other tasks
git switch main
git stash pop
# Method3:Cancel Edits
git restore .
git switch feature
# Method4:Carry, Modify, Toggle(If there is no conflict)
git switch feature
# If the changes do not conflict with the new branch,Changes will be saved
5. Deleting a Branch
(1) Delete merged branches
▶ Example: Safely Deleting a Branch
# Delete Merged Branches
git branch -d feature
# Output:
# Deleted branch feature (was a1b2c3d).
# Delete Multiple Branches
git branch -d feature1 feature2
# Delete a merged remote branch
git push origin --delete feature
# Or
git push origin :feature
(2) Force Deletion of a Branch
▶ Example: Forcibly Deleting Unmerged Branches
# Try deleting the unmerged branch
git branch -d feature
# Output Error:
# error: The branch 'feature' is not fully merged.
# If you are sure you want to delete it, run 'git branch -D feature'.
# Forced Deletion
git branch -D feature
# Output:
# Deleted branch feature (was a1b2c3d).
(3) Clean up remote branches
▶ Example: Cleaning up deleted remote branches
# View remote branch references
git branch -r
# Delete a local reference to a remote branch(Remotely Deleted)
git fetch -p
# Or
git remote prune origin
# View which remote branch references need to be cleaned up
git remote prune origin --dry-run
6. Best Practices for Branch Management
(1) Branch Strategy
Common branching strategies:
Git Flow:
graph TB
main[main Production] --> release[release Published]
release --> develop[develop Development]
develop --> feature[feature Features]
main --> hotfix[hotfix Thermal Repair]
style main fill:#d4edda
style develop fill:#fff3cd
style feature fill:#e1f5ff
style hotfix fill:#f8d7da
GitHub Flow:
- The main branch is always deployable
- Create a feature branch from
main - Create a pull request once development is complete
- Merge into main after review
(2) Branch Naming Recommendations
▶ Example: Branch Naming Conventions
# Feature Branch
feature/user-authentication
feature/shopping-cart
feature/payment-integration
# BugFix
fix/login-validation-error
fix/memory-leak-issue
# Thermal Repair
hotfix/security-vulnerability
hotfix/critical-bug
# Published
release/v1.0.0
release/v2.1.0
# Experiment
experiment/new-architecture
spike/performance-optimization
(3) Branch Workflow
▶ Example: Complete Branch Workflow
# 1. Create a feature branch from main
git checkout main
git pull origin main
git checkout -b feature/user-auth
# 2. Developing on a feature branch
echo "auth code" > auth.js
git add auth.js
git commit -m "feat: Add User Authentication"
# 3. Stay in touch withmainSynchronize
git fetch origin
git merge origin/main
# or userebase
git rebase origin/main
# 4. Push Feature Branch
git push -u origin feature/user-auth
# 5. Create Pull Request (on GitHub)
# 6. Delete feature branches after merging
git checkout main
git pull origin main
git branch -d feature/user-auth
git push origin --delete feature/user-auth
❓ FAQ
git log --oneline --graph --all to view the branch history graph, or use git merge-base <branch1> <branch2> to view the fork point between the two branches.main), deleting the branch will not result in the loss of that commit. If a commit exists only on the branch being deleted, it will be lost upon deletion, but can be recovered using git reflog.git switch is a new command introduced in Git 2.23 that is specifically designed for switching branches and offers clearer semantics. git checkout offers more comprehensive functionality, including switching branches, restoring files, and creating branches. We recommend using git switch to switch branches.git diff main feature to compare the differences between two branches, and use git diff main...feature to view the changes in the feature branch relative to the main branch.📖 Summary
- A branch is a movable pointer to a commit, and the cost of creating one is extremely low.
- View branch:
git branchto list local branches,-ato display all branches - Create a branch:
git branch <name>orgit switch -c <name>, then create and switch to it - Switch branches:
git switch <name>Update HEAD and the working directory - Delete branch:
git branch -dDelete a merged branch;-DForce delete - Branching strategies: Git Flow, GitHub Flow, etc. Choose the strategy that best suits your team.
📝 Exercises
-
Basic Exercise: Create a Git repository and create multiple branches (main, develop, feature). Commit different changes to each branch, and use
git branchandgit log --graphto view the branch structure. -
Advanced Exercise: Simulate a complete branching workflow: Create a feature branch from
main, develop the feature, keep it in sync withmain, and finally merge it back intomainand delete the feature branch. -
Challenge: Implement the Git Flow branching strategy in a real-world project by creating branches such as
develop,feature,release, andhotfix, and experience the complete branch management process.



