Branching
💻 GitCreate branches for features and fixes so main always stays releasable.
A branch is a cheap pointer to a commit. Use one per feature, fix or experiment so unfinished work never blocks a release.
- Create and switch branches.
- Name branches consistently.
- Delete branches safely.
Create and Switch
Modern Git uses git switch for branches and git restore for files. git checkout still works but does both.
git branch
git switch -c feature/login-form
git switch mainNaming
Use short, descriptive names: feature/login-form, fix/typo-footer, chore/update-deps. Consistent prefixes make branches sortable and searchable.
Delete
Delete merged branches to keep the list clean. Git refuses to delete unmerged work unless you force it.
git branch -d feature/login-form
git branch -D experiment/old-ideaPractice
- Create a branch called feature/navbar.
- Switch back to main and confirm the branch still exists.
- Delete a merged branch.
Hands-on exercise
Starting from main:
1. Create and switch to feature/about-page.
2. Add about.html and commit it.
3. Switch back to main.
4. Merge the branch.
5. Delete the branch.
Recap
Branches isolate work. Name them clearly, keep main releasable and delete branches once merged.
Example
git branch
git switch -c feature/login-form
git switch main
Exercises
1. Create and switch to feature/about-page.
2. Add about.html and commit it.
3. Switch back to main.
4. Merge the branch.
5. Delete the branch.
Expected Output
main contains about.html and the feature branch is gone.
Lesson Nav
Git Course
0/9 lessons completed
- 1 Why Version Control Matters Tutorial
- 2 Your First Repository Tutorial
- 3 Staging and Committing Tutorial
- 4 History, Diffs and Logs Tutorial
- 5 Branching Tutorial
- 6 Merging and Resolving Conflicts Tutorial
- 7 Remotes and Collaboration Tutorial
- 8 Undoing Mistakes Safely Tutorial
- 9 Project: A Team Workflow Project