Code / Command Line & Git / Unit 4: Branches & merging

Branches: safe side roads Branches: सुरक्षित साइड रोड

Lesson 9 of 12 · about 14 minutes · git branch, git switch -c (or checkout -b) and git switch.

⚡ 0 XP

Step 1 of 2

Make a branch

samosa (branch)mainmerge

Your website works. Now you want to try a winter menu — without breaking the working site. Make a branch: a side road where new commits go, while main stays safe.

  • git branch lists branches; * marks the one you are on.
  • git switch -c winter-menu creates a branch and switches to it. (Older tutorials write git checkout -b winter-menu — same thing.)

Watch the prompt: it shows (winter-menu) now.

🎯 Challenge: Cook up a winter menu +20 XP

In the chai-site repo, run git branch, then create and switch to a branch called winter-menu. On it, create winter.txt containing Kesar chai ₹20, and commit it (any message, e.g. Add winter menu).

Try twice to unlock the solution (0/2)

terminalCtrl ⏎

A safe practice terminal. Type a command and press Enter. Type help to list commands. Tab completes names, ↑ recalls the last command.

Files · ~ = /home/asha
  • chai-site/ ← you are here
  • menu.txt
Git timeline
mainc3e49a3 Add menuAdd menuHEAD→main
Checklist

Press Run to check your code.

  • Branch winter-menu exists
  • You are on winter-menu
  • winter-menu has 2 commits
  • winter.txt is committed with kesar chai
Lesson notes (text version)

1. Make a branch

Your website works. Now you want to try a winter menu — without breaking the working site. Make a branch: a side road where new commits go, while main stays safe.

  • git branch lists branches; * marks the one you are on.
  • git switch -c winter-menu creates a branch and switches to it. (Older tutorials write git checkout -b winter-menu — same thing.)

Watch the prompt: it shows (winter-menu) now.

Challenge: Cook up a winter menu

In the chai-site repo, run git branch, then create and switch to a branch called winter-menu. On it, create winter.txt containing Kesar chai ₹20, and commit it (any message, e.g. Add winter menu).

2. Switch between branches

git switch main goes back to the main road. Git swaps your files to match that branch: files that exist only on winter-menu vanish from the folder — but they are not lost! Switch back and they return.

Git refuses to switch if you have uncommitted changes, so your work is never silently thrown away. Commit first, then switch.

Challenge: Back to main

You are on winter-menu. Switch back to main with git switch main, then run ls — notice winter.txt is not there on main. Stay on main.

Search by task, format or tool name.