Skip to main content

Git Branch Management: The Foundation of Team Collaboration

Branches are like parallel universes โ€” you can create a new universe to experiment in without affecting the main one.

Why Do You Need Branches?โ€‹

Imagine you're writing a paper. Halfway through, you suddenly have a new idea, but you don't want to mess up what you've already written. What do you do?

Make a copy and go wild with it. That's essentially what a branch is.

In Git, branches let you:

  • Safely experiment with new features: Messed up? Just delete the branch โ€” the main code stays untouched
  • Develop with multiple people simultaneously: You work on login, I work on the homepage โ€” no interference
  • Keep the main branch stable: The main branch is always a working version

Understanding Branches with a Train Track Analogyโ€‹

Think of a Git repository as a railway system:

main: โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ—โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ—โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ—
\ /
feature-login: โ—โ•โ•โ•โ•โ•โ—
/
feature-home: โ•โ•โ•โ•โ•โ•โ—โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ—
  • main is the main line โ€” the official train that passengers (users) ride
  • feature-login is a branch line โ€” a construction crew is building a new station on it, and once done, it merges into the main line
  • feature-home is another branch line โ€” another crew working in parallel

Work on branch lines doesn't affect the main line's normal operations. When it's done, just connect it.

Branch Types at a Glanceโ€‹

Branch NamePurposeExample
mainProduction code, always deployableRelease versions
developMain development line, feature integrationDaily development
feature/*New feature developmentfeature/login
bugfix/*Bug fixesbugfix/header-crash
hotfix/*Emergency production fixeshotfix/payment-error

Basic Branch Operationsโ€‹

Creating Branchesโ€‹

# View current branches
git branch

# Create a new branch
git branch feature/login

# Switch to the new branch
git checkout feature/login

# Create and switch (common shortcut)
git checkout -b feature/login

# New syntax in Git 2.23+ (recommended)
git switch -c feature/login

Viewing All Branchesโ€‹

# Local branches
git branch

# Including remote branches
git branch -a

# View last commit of each branch
git branch -v

Deleting Branchesโ€‹

# Delete a merged branch
git branch -d feature/login

# Force delete (even if not merged)
git branch -D feature/login

Merging Branchesโ€‹

merge โ€” The Most Common Way to Mergeโ€‹

# Switch back to main
git switch main

# Merge feature/login in
git merge feature/login

There are two merge modes:

Fast-Forward Merge โ€” main has no new commits, so it just "fast-forwards":

Before:
main: A โ†’ B
\
feature: C โ†’ D

After (fast-forward):
main: A โ†’ B โ†’ C โ†’ D

Three-Way Merge โ€” both main and the branch have new commits:

Before:
main: A โ†’ B โ†’ E
\
feature: C โ†’ D

After:
main: A โ†’ B โ†’ E โ†’ F (merge commit)
\ /
feature: C โ†’ D

rebase โ€” A Cleaner Historyโ€‹

# On the feature branch
git rebase main

Rebase "moves" your commits to after the latest position on main, making the history a straight line:

Before rebase:
main: A โ†’ B โ†’ E
\
feature: C โ†’ D

After rebase:
main: A โ†’ B โ†’ E โ†’ C' โ†’ D'

merge vs rebase โ€” which to choose:

  • merge preserves real history โ€” good for shared branches
  • rebase gives a cleaner history โ€” good for personal branches
  • Golden rule: never rebase a public branch that's already been pushed to a remote

Pull Request (PR) Basicsโ€‹

Pull Requests are the core workflow for code review. After you finish a feature, you request the team to "pull" your code into the main branch.

Creating a PR on GitHubโ€‹

# 1. Push your branch to the remote
git push origin feature/login

# 2. Go to the GitHub repo page โ€” you'll see a yellow banner
# Click "Compare & pull request"

# 3. Fill in the PR description
# - What you did
# - Why you did it
# - How to test it

# 4. Wait for review, then merge

PR Description Templateโ€‹

## What I Did
Added user login functionality, supporting email/password login

## Why
Users need an account system to save their data

## How to Test
1. Visit /login
2. Enter email and password
3. Click login โ€” should redirect to homepage

## Screenshots
(Attach screenshots)

Handling Merge Conflictsโ€‹

Conflicts happen when Git can't automatically merge โ€” two people changed the same line of code.

What a Conflict Looks Likeโ€‹

<<<<<<< HEAD
const title = "Welcome back";
=======
const title = "Hello, welcome";
>>>>>>> feature/login
  • <<<<<<< HEAD to =======: content from your current branch
  • ======= to >>>>>>> feature/xxx: content from the branch being merged

Resolution Stepsโ€‹

# 1. Start the merge, encounter conflict
git merge feature/login
# CONFLICT (content): Merge conflict in src/app/page.tsx

# 2. See which files have conflicts
git status
# both modified: src/app/page.tsx

# 3. Open the file, manually choose what to keep
# Delete the <<<<<<< ======= >>>>>>> markers, keep the correct code

# 4. Mark as resolved
git add src/app/page.tsx

# 5. Complete the merge
git commit -m "merge: merge feature/login, resolve conflicts"

Resolving Conflicts with VS Codeโ€‹

VS Code highlights conflicts and provides buttons:

  • Accept Current Change โ€” keep the current branch
  • Accept Incoming Change โ€” keep the branch being merged
  • Accept Both Changes โ€” keep both sides
  • Compare Changes โ€” compare differences

Branch Naming Conventionsโ€‹

A good branch name tells you what it's about at a glance:

# โœ… Good names
feature/user-login
feature/payment-stripe
bugfix/header-not-showing
hotfix/fix-payment-crash
chore/update-dependencies

# โŒ Bad names
my-branch
test
fix
new-feature
temp

Naming format: type/short-description

TypePurposeExample
feature/New featuresfeature/user-profile
bugfix/Bug fixesbugfix/login-error
hotfix/Emergency fixeshotfix/payment-crash
chore/Housekeepingchore/cleanup-deps
docs/Documentationdocs/api-guide
refactor/Refactoringrefactor/auth-module

GitHub Flow vs Git Flowโ€‹

Git Flow โ€” Traditional Large Projectsโ€‹

main โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
โ”‚ โ†‘
โ”‚ โ”Œโ”€ release/1.0 โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ”‚ โ”‚
โ””โ”€โ”€ develop โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
โ”‚ โ”‚
โ”‚ โ””โ”€ feature/B
โ””โ”€ feature/A

Best for: large teams, projects with fixed release cycles

main โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
โ”‚ โ†‘ โ†‘
โ”œโ”€ feat/A โ”˜ โ”‚
โ”‚ โ”‚
โ””โ”€ feat/B โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

The rules are simple:

  1. main branch is always deployable
  2. New features branch off from main
  3. Submit a PR when development is done
  4. Merge to main after code review passes
  5. Deploy immediately after merging

If you're a beginner, just use GitHub Flow โ€” it's simple and gets the job done.

Hands-On: Developing a Login Feature with Branchesโ€‹

Let's walk through the complete workflow:

# 1. Make sure you're on main with the latest code
git switch main
git pull

# 2. Create a feature branch
git switch -c feature/user-login

# 3. Start coding
# Create the login page
# src/app/login/page.tsx
// src/app/login/page.tsx
export default function LoginPage() {
return (
<div className="flex min-h-screen items-center justify-center">
<form className="w-96 space-y-4 rounded-lg border p-8">
<h1 className="text-2xl font-bold">Login</h1>
<input
type="email"
placeholder="Email"
className="w-full rounded border p-2"
/>
<input
type="password"
placeholder="Password"
className="w-full rounded border p-2"
/>
<button className="w-full rounded bg-blue-500 p-2 text-white">
Login
</button>
</form>
</div>
);
}
# 4. Commit the code
git add .
git commit -m "feat: add login page UI"

# 5. Continue development, add login logic
# ... write code ...

git add .
git commit -m "feat: implement email/password login"

# 6. Push to remote
git push -u origin feature/user-login

# 7. Create a PR on GitHub
# Fill in description, wait for review

# 8. After review passes, merge
# Click "Merge pull request" on the GitHub page

# 9. Sync locally
git switch main
git pull

# 10. Delete the feature branch
git branch -d feature/user-login
git push origin --delete feature/user-login

Quick Reference: Common Commandsโ€‹

# Branch operations
git branch # View local branches
git branch -a # View all branches
git switch -c feature/xxx # Create and switch to a branch
git switch main # Switch branch
git branch -d feature/xxx # Delete a branch

# Merge operations
git merge feature/xxx # Merge a branch
git rebase main # Rebase onto main

# Sync operations
git push -u origin feature/xxx # Push a new branch
git pull # Pull latest code

# Conflict handling
git status # View conflicting files
# After manually resolving conflicts
git add . # Mark as resolved
git commit # Complete the merge

Summaryโ€‹

  • Branches are safe experiment zones โ€” mess up and just delete them
  • Beginners should use GitHub Flow โ€” simple and sufficient
  • Branch names should be meaningful โ€” feature/xxx, bugfix/xxx
  • Conflicts aren't scary โ€” just pick the right code manually
  • PRs are the core of team collaboration โ€” write good descriptions, be patient with reviews

Branch management isn't some advanced technology โ€” it's just a good habit. The more you use it, the more natural it becomes.