Pular para o conteúdo principal

Branches — Work in Parallel Without Risk

Module 02 60 min

Section Objectives

  • Understand what a branch is and why it's fundamental
  • Create, switch, rename, and delete branches
  • Understand the difference between HEAD and a branch
  • Know the common branch naming conventions

What is a Branch?

A branch is simply a lightweight movable pointer to a commit. When you create a new branch, Git just creates a new pointer — it doesn't copy anything.

When you create a new branch and make a commit:

Why Use Branches?

Without BranchesWith Branches
Work directly on mainIsolate each feature/fix
Breaking changes break everyoneBreaks are isolated
Impossible to work in parallelN developers work simultaneously
No way to "try something"Experiment freely, delete if fails
Manual code reviewsPRs per branch
Branches are free

Creating a branch in Git is almost instantaneous and has almost zero cost in terms of disk space. Use them generously.


Managing Branches

Create a Branch

# Create a branch (without switching)
git branch feature/login

# Create AND switch to the branch (modern syntax)
git switch -c feature/login

# Create AND switch (old syntax, still widely used)
git checkout -b feature/login

# Create a branch from a specific commit or branch
git switch -c hotfix/bug-42 main
git switch -c experiment v1.0 # From a tag

Switch Branches

# Modern syntax (recommended)
git switch main
git switch feature/login

# Old syntax (still works)
git checkout main
git checkout feature/login

# Switch to last branch
git switch -
git checkout - # Equivalent to cd -

View Branches

# List local branches
git branch

# List with last commit
git branch -v

# List local + remote branches
git branch -a

# List only remote branches
git branch -r

# List merged branches (safe to delete)
git branch --merged

# List unmerged branches
git branch --no-merged

Rename a Branch

# Rename current branch
git branch -m new-name

# Rename a specific branch
git branch -m old-name new-name

# Rename main → main (for repos initialized with "master")
git branch -m master main

Delete a Branch

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

# Force delete (even if unmerged — CAREFUL!)
git branch -D feature/abandoned

# Delete a remote branch
git push origin --delete feature/login

Branch Naming Conventions

Clear, consistent naming is a professional practice:

PrefixUse CaseExamples
feature/New featurefeature/user-login, feature/payment
fix/ or bugfix/Bug fixfix/email-validation, bugfix/crash-ios
hotfix/Urgent production fixhotfix/security-vulnerability
release/Release preparationrelease/v1.2.0
chore/Maintenancechore/update-dependencies
refactor/Code refactoringrefactor/auth-module
docs/Documentationdocs/api-guide
test/Test additionstest/login-unit-tests
# Naming Best Practices
✅ feature/user-authentication
✅ fix/cors-headers-missing
✅ hotfix/sql-injection-v2.1
✅ release/v2.0.0

❌ fix # Too vague
❌ MY_FEATURE # No uppercase or underscores
❌ feature/adding-the-new-user-login-form-for-admin # Too long
❌ fix-bug # No prefix

Visualizing Branches


Understanding HEAD in Detail

HEAD is a special file in .git/HEAD that tells Git where you currently are:

# View HEAD content
cat .git/HEAD
# In normal state: ref: refs/heads/main
# In detached state: abc1234def5678...

# View what HEAD points to
git symbolic-ref HEAD # refs/heads/main
git rev-parse HEAD # Full commit hash

Detached HEAD State

"Detached HEAD" occurs when HEAD points directly to a commit instead of a branch:

# This causes detached HEAD
git checkout abc1234

# Warning: You are in 'detached HEAD' state.
# You can look around, make experimental changes and commit them,
# and you can discard any commits you make in this state
# without impacting any branches by switching back to a branch.

# Return to a branch
git switch main
git checkout main

# Save your work from detached HEAD
git switch -c my-experiment # Create branch from current state

Summary of Key Commands

CommandDescription
git branchList local branches
git branch -aList all branches
git branch <name>Create a branch
git switch -c <name>Create and switch
git switch <name>Switch branch
git switch -Back to last branch
git branch -d <name>Delete merged branch
git branch -D <name>Force delete
git branch -m <name>Rename current branch

Next Steps