Branches
Understand the idea
A branch is a movable label on a commit. Switching branches moves the label, it does not copy the files.
The idea
A is a name that points at one commit. says which name you are using. When you commit, that name moves. git branch topic creates the note. git switch topic moves HEAD onto it. The files on disk become that commit's snapshot.
What changes
Creating a branch does not change the until you switch and the snapshots differ. Committing on topic moves only topic. main stays where it was, which is why the graph forks. Checking out a raw hash detaches HEAD: the next commit has a parent, but no branch name points at it until you create one.
| Action | What moves | What stays |
|---|---|---|
| git branch topic | A new label appears on the current commit | The files |
| git switch topic | HEAD and the working tree | Other branch labels |
| git commit on topic | The topic label | main |
The command
git branch topic, then git switch topic, then write, add, and commit. Switch back with git switch main. The graph shows topic on the new commit and main on the old one. git switch -c experiment creates and switches in one step. git checkout accepts the same idea.
The mistake
Check your graph
topic and main should point at different commits, and HEAD should sit on the branch you switched to. The files in git show topic match the commit, not a private copy of the folder.
Your repository
Create a branch, commit on it, and switch back. Watch the label and HEAD move.
Step 1
Name the current commit
A branch is a label. This command puts topic on the same commit as main. It does not copy files.
Step 2
Switch onto that label
HEAD now uses topic. If topic already exists, run git switch topic instead.
Step 3
Edit on this branch
Create notes.txt in the editor and set the text to: topic work
Step 4
Stage and commit on topic
Only the topic label moves. main stays on the previous commit.
Step 5
Store the topic snapshot
The graph forks here. topic points at the new commit.
Step 6
Return to main
The working tree becomes main’s snapshot. notes.txt from topic is no longer the checked-out file.
Step 7
Read both labels
The graph shows topic and main on different commits.
The editor edits the working tree. Start a repository and the files appear here.
Commit graph
No commits yet. git init creates the first one.
Quiz
A quick check for understanding. Retry as often as you like; your learning path stays open.
Ready to call this one understood?
Mark this lesson complete to earn 100 XP. You can always revisit it.