Branching Basics
Branches are Git's killer feature. They let you work on different things simultaneously without affecting each other.
What is a Branch?
A branch is simply a pointer to a commit. When you create a new branch, you're creating a new timeline that can diverge from the main one.
Think of it like a "choose your own adventure" book - at any point, you can take a different path!
The Main Branch
When you create a repository, Git creates a default branch called main (or master in older repos). This is typically your primary timeline.
Creating a Branch
To create a new branch:
git branch feature-login
This creates a new branch pointing to your current commit, but doesn't switch to it.
Switching Branches
To move to a different branch:
git switch feature-login
Or create and switch in one command:
git switch -c feature-login
Understanding HEAD
HEAD is a special pointer that tells Git which branch (and commit) you're currently on. It's like a "you are here" marker on a map. main
│
▼
○──○──○──○ (HEAD)
│
└──○──○ feature-login
When HEAD points to main, any new commits will be added to the main branch.
Branch Workflow
A typical workflow looks like this:
- Create a branch for your new feature
- Switch to that branch
- Make commits on the feature branch
- Switch back to main when done
- Merge your feature (next lesson!)
Try It Out!
Use the interactive visualizer to experiment with branches.
Visual Mode: Click "Create Branch" and drag HEAD between branches Command Mode: Usegit branch and git switch commands
Watch how the graph changes as you create branches and make commits!