New! Explore our Programming Academy and AI Tutor - learn to code from scratch, free to start. Explore Now
Programming› Git› Branching

Branching

💻 Git

Create branches for features and fixes so main always stays releasable.

Lesson 5 of 9 Tutorial
0 of 9 completed

A branch is a cheap pointer to a commit. Use one per feature, fix or experiment so unfinished work never blocks a release.

By the end of this lesson you can:
  • 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 main

Naming

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-idea
Warning Switching branches can change files in your working directory. Commit or stash changes first.

Practice

  1. Create a branch called feature/navbar.
  2. Switch back to main and confirm the branch still exists.
  3. 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

Exercise 1. Feature branch workflow Easy
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.
Git exercise-1.txt
Expected Output
main contains about.html and the feature branch is gone.

Lesson Nav

Lesson sections