Merge branches
Combine your work
⏱ Est. ~12 min
01 · Read
Merging is how you combine changes from one branch into another. When your feature branch is ready, you switch back to main and merge your branch in. git automatically combines the changes for you.
💡 Picture thisMerging is like combining two versions of a document. You and your coworker each had your own copies. Now you sit down and merge the good bits from both into one main version. Most of the time, git handles it automatically.
Key points
- Switch to the target branch first (git checkout main), then run git merge branch-name
- If there are no conflicts, git automatically combines the changes
- After merging, all the commits on the feature branch become part of main's history
02 · Terminal exercise
Before we can branch and merge, we need a repo with a commit. Initialize git first.
(This section is interactive — please enable JavaScript.)
03 · Terminal exercise
Stage all the files.
(This section is interactive — please enable JavaScript.)
04 · Terminal exercise
Save your first commit. If you see "nothing to commit, working tree clean," you've already committed successfully — any git commit command will take you to the next step.
(This section is interactive — please enable JavaScript.)
05 · Fill in the blank
To switch from a feature branch back to the main branch, type: git checkout ___
06 · Terminal exercise
Create a new branch for your feature. You're going to add a footer to the website.
(This section is interactive — please enable JavaScript.)
07 · Terminal exercise
Add a footer to the HTML file. >> appends text to the end of the file without erasing what was there.
(This section is interactive — please enable JavaScript.)
08 · Read
Halfway checkpoint — branch is ready
You've finished the first half: init the repo → first commit → create a feature branch → add the footer.
At this point your git status has changes that haven't been committed to the add-footer branch yet. The second half will commit that change, switch back to main, then do the most important step: merge.
Why do branch + merge matter? Because this is the daily workflow of professional teams — everyone works on their own branch, then merges into main when ready. What you're learning today is the real development rhythm.
Key points
- First half covered: git init / git add / git commit / git checkout -b
- Second half covers: committing your branch changes, switching back to main, merging into main
- Before merging, always switch to the branch you want to merge into (usually main)
09 · Terminal exercise
Stage your changes.
(This section is interactive — please enable JavaScript.)
10 · Quiz
You're on the main branch. What does git merge feature do?
- Deletes the feature branch
- Copies the feature branch
- Combines the feature branch's changes into main
- Switches you to the feature branch
11 · Terminal exercise
Commit the footer feature to this branch. If you see "nothing to commit, working tree clean," you've already committed successfully — any git commit command will take you to the next step.
(This section is interactive — please enable JavaScript.)
12 · Terminal exercise
To merge, you first switch to the branch you want to merge into. Switch back to main.
(This section is interactive — please enable JavaScript.)
13 · Terminal exercise
Merging brings changes from another branch into your current branch. Because you're on main, merging add-footer brings the footer into main.
(This section is interactive — please enable JavaScript.)
14 · Quiz
You're on the add-footer branch and want to merge it into main. What's the correct order?
- Run git merge main while on add-footer
- Run git merge add-footer while on add-footer
- Run git push, GitHub will auto-merge
- Switch to main with git checkout main, then run git merge add-footer
Other lessons in this chapter
⚠ The full interactive experience needs JavaScript. Please enable it and reload.
※ This is an independent Traditional Chinese teaching project — not an official Anthropic product. Claude™ is a trademark of Anthropic, PBC.