Learning objectives
By the end of this module, you should be able to:
- Create and switch branches with git switch.
- Merge a feature branch into main.
- Explain when a fast-forward is possible.
Branches are lightweight references
Creating a branch creates another movable name for a commit; it does not duplicate the whole repository. New commits advance the currently checked-out branch.
Fast-forward versus merge commit
If main has not diverged, Git can move the main reference forward to the feature tip. If both sides have new commits, a merge usually creates a new commit with multiple parents after conflicts are resolved.
Integrate from the target branch
A common local sequence is switch to the target branch, update it as required, then merge the feature branch. On GitHub, teams typically integrate through pull requests rather than merging directly on protected branches.
Worked examples
See the idea in practice.
Branch and merge
git switch -c feature/readme
printf "feature\n" >> README.md
git add README.md && git commit -m "Document feature"
git switch main
git merge feature/readme
- The feature commit is isolated until merge.
- If main did not move, the merge can fast-forward.
Hands-on lab
Lab — Develop on a feature branch
Goal: Create a feature branch, commit work and merge it into main.
Before you start
- Git 2.23 or newer is recommended so the labs can use git switch and git restore.
- Run labs in a disposable working directory; the included validator creates and removes its own temporary repository.
STEP 1Create base repository
mkdir -p ~/gnu-git-labs/module05 && cd ~/gnu-git-labs/module05
git init -b main
git config user.name "GNU Learner"
git config user.email "learner@example.invalid"
printf "base\n" > app.txt
git add app.txt && git commit -m "Create base"
STEP 2Create a feature branch
git switch -c feature/banner
printf "banner\n" >> app.txt
git commit -am "Add banner"
STEP 3Return to main and merge
git switch main
git merge feature/banner
STEP 4Inspect graph
git log --oneline --graph --decorate --all
Verify
- main contains the feature change.
- feature/banner still names the feature commit until deleted.
Expected outcome
- The commit graph explains whether Git fast-forwarded or created a merge commit.
If it fails
- If switch is blocked, inspect uncommitted changes before forcing anything.
- If merge reports a conflict, do not use --hard reset blindly; move to the conflict-resolution workflow.
Real-world connectionShort-lived branches support review and controlled integration without destabilizing the shared main line.
Avoid these traps
Common mistakes
- Doing unrelated work directly on main in a team repository.
- Believing deleting a merged branch deletes its commits immediately.
- Forcing a branch switch that would discard uncommitted work.
Knowledge check
Can you explain it without looking back?
Open each answer only after you have tried to answer the question yourself.
1What is a Git branch technically?
A movable reference to a commit.
2When can a merge fast-forward?
When the target branch is an ancestor of the branch being merged and has not diverged.
3Where do new commits go after git switch feature/x?
They advance the feature/x branch.