Master the concept of branches in Git and learn how they help manage code versions
Understanding Git branches, their purpose, and how they help manage different versions of your code.
A branch in Git is a lightweight movable pointer to a commit. It represents an independent line of development, allowing you to work on different features or fixes without affecting the main codebase.
- •
Branches are references to specific commits
- •
The default branch is usually called 'main'
- •
Each branch maintains its own commit history
- •
Branches can be merged back together
- •
Developing new features
- •
Fixing bugs
- •
Experimenting with code
- •
Managing releases
See how branches work in practice with this interactive visualization:
Create a new branch for each feature or bug fix. Merge back to main when complete.
# Each feature gets its own branch
# Time flows from left to right →
main ●───●───●───────●───────● # Main branch
╲ ╱
feature ●───●───● # Feature branch
└── New feature development
A more structured approach with multiple branch types: main, develop, feature, release, and hotfix branches.
# GitFlow uses multiple branch types
# Time flows from left to right →
main ●─────────────●───────● # Production code
│ ↑ ↑
develop ●──●──●──●──●│───────● # Development branch
↑ ↑│
feature1 ●──● │ # Feature branches
│
feature2 ●───● # Multiple features
hotfix ●─────● # Quick fixes
Developers work directly on the main branch or use short-lived feature branches.
# Frequent merges to main branch
# Time flows from left to right →
main ●───●───●───●───●───●───● # Main/trunk branch
│ ╱ │ ╱ │ ╱ │ ╱ │ # Quick merges
short1 ● │ │ │ │ # Short-lived
│ │ │ │ │ # feature branches
short2 │ ● │ │ │
│ │ │ │
short3 │ ● │ │
│ │ │
short4 │ ● │ # Each branch lives
│ │ # for a short time
short5 │ ●
- #### Keep Branches Focused
Each branch should represent a single feature, bug fix, or task.
- #### Use Descriptive Names
Branch names should clearly indicate their purpose (e.g., feature/user-auth).
- #### Regular Updates
Keep branches up to date with their parent branch to avoid complex merges.
- #### Clean Up
Delete branches after they're merged to keep the repository clean.
Now that you understand branch concepts, in the next lesson you'll learn:
- How to create and switch between branches
- Basic branch management commands
- How to delete and rename branches