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.

100%
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:

That's why Git creates branches so quickly.

BASH
# View Branch Files
cat .git/refs/heads/main

# Output:
# a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0

(3) HEAD Pointer

HEAD is a special pointer that points to the current branch:

100%
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


2. View Branches

(1) View local branches

▶ Example: List branches

BASH
# 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

BASH
# 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

BASH
# 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

BASH
# 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

100%
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

TEXT
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

BASH
# 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

100%
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:

  1. Update the HEAD to point to the new branch
  2. Update the staging area to reflect the status of the new branch
  3. Update the files in the workspace with the contents of the new branch

(3) Handling Unsaved Changes

▶ Example: Processing Changes Before Switching Branches

BASH
# 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

BASH
# 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

BASH
# 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

BASH
# 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:

100%
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:

(2) Branch Naming Recommendations

▶ Example: Branch Naming Conventions

BASH
# 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

BASH
# 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

Q Will the original branch be affected after creating a new branch?
A No. Creating a branch simply creates a new pointer that points to the current commit; the original branch remains completely unaffected. The two branches can evolve independently.
Q How can I see which branch a branch was created from?
A Use 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.
Q Will commits be lost after deleting a branch?
A If a commit has been referenced by another branch (e.g., if it has been merged into 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.
Q What is the difference between "checkout" and "switch"?
A 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.
Q How do I compare differences between branches?
A Use 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


📝 Exercises

  1. Basic Exercise: Create a Git repository and create multiple branches (main, develop, feature). Commit different changes to each branch, and use git branch and git log --graph to view the branch structure.

  2. Advanced Exercise: Simulate a complete branching workflow: Create a feature branch from main, develop the feature, keep it in sync with main, and finally merge it back into main and delete the feature branch.

  3. Challenge: Implement the Git Flow branching strategy in a real-world project by creating branches such as develop, feature, release, and hotfix, and experience the complete branch management process.

Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏