Git Branch Introduction
In this page:
What is a Git Branch?
A branch is Git's mechanism for an independent line of development -- under the hood it's just a movable pointer to a commit, which is why creating one is instant and cheap, letting you experiment freely without risking your stable code.
Example: What is a Git Branch?
git branch feature-login
Why Use Branches?
Branches let separate pieces of work happen in isolation: one developer builds a feature, another fixes a bug, and neither set of unfinished changes can accidentally break what's running in production until it's deliberately merged in.
Example: Why Use Branches?
git branch bugfix-header
git branch feature-payment
The Default Branch
Every repository starts with one default branch -- conventionally named main (or the older master) -- which by convention represents the project's current stable, deployable state, even though Git treats it as just another branch technically.
Example: The Default Branch
git branch
# * main
Local vs Remote Branches
A local branch exists only on your own machine until you push it; a remote branch is one hosted on a server like GitHub. You fetch remote branches to see what teammates have done before deciding whether to merge those changes locally.
Example: Local vs Remote Branches
git branch
git branch -r
Basic Branch Workflow
The everyday branch workflow is: create a branch for your task, switch onto it, commit your changes there, merge those commits back into main once finished, then delete the now-unneeded branch to keep the repository tidy.
Example: Basic Branch Workflow
git branch feature-x
git checkout feature-x
git commit -m "Add feature"
git checkout main
git merge feature-x
git branch -d feature-x
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: