Git without fear · Chapter 13 of 28

Branches

By the end of this chapter you can make a branch for new work, commit on it without touching main, and rename and delete branches safely. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Branches
  • 5 tasks
  • Git
  • In the all labs plan

The first 2 chapters of Git without fear are free. This one comes with the all labs plan.

Why you need this

Anjali has dropped festive.txt, the Diwali menu, into the repository folder. Meera wants it ready but not live until next week, so it cannot go on main: everything on main goes out to the app. And git branch shows two more jobs:

  feature/snaks
  fix-price-typo
* main

Bunty named his snacks branch feature/snaks, and Ravi's fix-price-typo was merged into main on Tuesday, so it can go.

So far every commit you made went on main. Commit the festive menu there and it is live today; leave it uncommitted and it is saved nowhere, one careless git add . away from landing on main anyway. A branch is a separate line of commits you can work on, and switch away from, without changing main at all.

What you build: 5 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    A festive-menu branch from main

    Anjali's Diwali menu needs its own branch. Make a branch called festive-menu from main.

    1. Run git status. You are on main.

    2. Make the branch festive-menu and switch to it, in one command.

    3. Do not commit anything on main.

    To pass: festive-menu starts from the newest commit on main, and you are on festive-menu when you press Check. The newest commit on main is still "Fix the cutting chai typo in prices".

  2. Task 2

    Commit festive.txt on festive-menu, not on main

    Commit Anjali's festive.txt on the new branch only.

    1. Make sure you are on festive-menu.

    2. Stage festive.txt by its name.

    3. Commit it with a message about the Diwali menu.

    To pass: festive-menu has a commit with festive.txt (with Kesar chai 60). main does not have festive.txt at all.

  3. Task 3

    Rename feature/snaks to feature/snacks

    Bunty made a typo in his branch name. Rename the branch.

    1. Run git branch to list the branches.

    2. Rename feature/snaks to feature/snacks with git branch and its rename option.

    3. List the branches again to see the new name.

    To pass: feature/snacks exists, and its newest commit is "Add samosa and vada pav". feature/snaks does not exist any more.

  4. Task 4

    Delete the merged fix-price-typo, the safe way

    fix-price-typo is already merged into main. Delete the branch the safe way.

    1. Run git branch -d fix-price-typo. -d deletes a branch only when its work is merged.

    2. Do not use -D or --force, for any branch.

    To pass: fix-price-typo is gone, and you deleted it with git branch -d, in the chapter folder, and it worked. You never typed git branch -D, or a delete with --force or -f, in this chapter, not even one that failed. "Fix the cutting chai typo in prices" is still on main.

  5. Task 5 · interview question

    Interview: a branch is a name for a commit

    Interview question: what is a branch? Show it with a hash.

    1. Do this after your festive commit (task 2).

    2. Find the commit hash that festive-menu points to. Short or full is fine.

    3. Save only that hash in answer.txt, in the chapter folder.

    To pass: answer.txt has the hash festive-menu points to now, and nothing else. If you commit on festive-menu again, save the new hash.

What you will learn

  • What a branch is: a name that points at one commit, and moves when you commit.
  • git switch -c: create a branch and move onto it.
  • git branch -m: rename a branch.
  • git branch -d: delete a branch, only if its work is merged.

More in Branches

Build "Branches" yourself

Your first week on Tapri’s team, and every Git situation it throws at you. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Git without fear are free. This one comes with the all labs plan.