In Git version control systems, branches are a crucial tool for managing code. They allow us to develop new features or fix bugs in parallel without compromising the stability of the main program, much like creating temporary “side roads” off a main highway—once completed, we return to the main road. Among these, the main/master branch and feature branches are the two most core types. Their differences and usage scenarios directly impact project development efficiency and code quality.

Why Use Branches?

Imagine everyone driving on a single “main road” (e.g., directly writing code on the main branch). If someone makes a mistake (e.g., submitting incomplete code or causing test failures), the entire project’s progress could stall. Branches act like “highway ramps,” allowing each person to develop on their own ramp (feature branch) without interference. Once completed, they merge back into the main road (main branch), keeping the main road畅通 (unblocked) at all times.

I. Main Branch (main/master): The Project’s “Anchor”

1. What is the Main Branch?

The main branch is the project’s “core trunk,” typically named main (recommended for modern Git projects) or master (common in older projects). It stores the stable version of the project that can be deployed to production at any time, like a fully tested “finished warehouse.”

2. Key Characteristics of the Main Branch

  • Stable and Reliable: Code on the main branch must undergo thorough testing to ensure no bugs and consistent functionality (e.g., code version deployed on a website).
  • Only Accepts Merges: Never write code directly on the main branch! It is “read-only” and can only serve as the target for merging feature branches (e.g., after a new feature is developed, merge it into the main branch).
  • Long-Lasting: The main branch continuously evolves with project iterations and remains the ultimate source of truth for the project.

3. Functions of the Main Branch

  • Production Environment Baseline: Code deployed to production must be pulled from the main branch to ensure users run on a stable version.
  • Merge Target: All new features and bug fixes must eventually merge into the main branch to “officially go live.”

II. Feature Branches: Temporary “Side Roads” for New Features

1. What are Feature Branches?

Feature branches are temporary branches created for developing new features or fixing bugs, typically named feature/xxx (e.g., feature/login) or bugfix/xxx (e.g., bugfix/error-handling). They are created from the main branch, merged back into it upon completion, and then deleted.

2. Key Characteristics of Feature Branches

  • Temporary Isolation: Development on a feature branch does not directly affect the stability of the main branch (e.g., untested code in the feature branch won’t break the main branch).
  • Focused Development: A single feature branch handles one task (e.g., only the login function), avoiding code clutter from multiple features on one branch.
  • Disposable Upon Completion: The lifecycle of a feature branch ends after merging back into the main branch; it can be deleted to keep the repository organized.

3. Functions of Feature Branches

  • Parallel Development: Multiple team members can work on different feature branches simultaneously (e.g., A works on login, B on registration) without interference.
  • Risk Isolation: If a feature branch has issues, the main branch remains stable. Only after testing can the branch be merged, ensuring the main branch stays “clean and usable.”

III. Main Branch vs. Feature Branch: Core Differences

Comparison Main Branch (main/master) Feature Branch (e.g., feature/xxx)
Purpose Stable production version Temporary task for new features/bug fixes
Source The ultimate source of all branches Must be created from the main branch
Code State Stable, functional, no incomplete code Temporary, possibly untested/incomplete
Modification Only accepts merges (no direct edits) Free development, commits, and testing
Lifecycle Long-lasting (evolves with the project) Temporary (deleted after merging)

IV. Must-Know: Correct Branch Usage Scenarios

❌ Bad Example: Direct Development on the Main Branch

If you write code directly on the main branch (e.g., halfway through a “shopping cart” feature) and suddenly need to fix a critical bug, merging the bug fix directly into the main branch will mix the incomplete cart code with the bug fix. This complicates subsequent testing.

✅ Good Example: Feature Branch Workflow

  1. Create a Feature Branch from the Main Branch:
    First, ensure the main branch is up-to-date, then create the feature branch:
   git checkout main   # Switch to the main branch
   git pull            # Fetch the latest main branch code
   git checkout -b feature/shopping-cart  # Create and switch to the shopping cart branch
  1. Develop on the Feature Branch:
    Write code and commit changes on feature/shopping-cart:
   git add .           # Stage changes
   git commit -m "Complete shopping cart UI"  # Commit changes
  1. Merge Back to the Main Branch After Testing:
    Once complete, merge into the main branch:
   git checkout main   # Switch back to the main branch
   git merge feature/shopping-cart  # Merge the feature branch into main
   git push origin main  # Push to the remote repository
  1. Delete the Feature Branch:
    After merging, the feature branch’s task is complete and can be deleted:
   git branch -d feature/shopping-cart  # Delete the local feature branch
   git push origin --delete feature/shopping-cart # Delete the remote feature branch

Conclusion: The Main Branch is the Foundation, Feature Branches are the “Wings”

  • Main Branch: The project’s “anchor,” must remain stable and serves as the source for end-user versions.
  • Feature Branches: The “experimental field” for development, enabling safe exploration and testing of new features without affecting the main branch.

Remember: Never write incomplete code on the main branch! Feature branches should be merged and deleted once used. This ensures your project remains efficient and stable. Next time you develop, try isolating tasks with branches!

Xiaoye