Level 7

Branch & switch

Create a new branch, switch between branches, and watch two storylines split apart.

Excellent progress. You have built a strong foundation already. So far, every page has gone into one section of the scrapbook called main, the default section every new scrapbook starts with.

Most scrapbookers, and programmers, like to create extra sections so they can try out new ideas without messing up their main storyline. Git makes that easy. Those sections are called branches, and in nerd-speak making one is lightweight, which just means quick and simple.

Step 1 of 15
0 xp

What a branch really is

A branch is basically a section marker labelling one scrapbook page, one commit. That is why the green main marker has always sat on top of the latest page you made.

Each page connects back to the one before it, forming a chain, and Git follows those links to read the full story. So you can think of a branch as its own storyline of connected pages that form a section in the book, while Git itself treats it as nothing more than a marker on the most recent page of that storyline.

Making a section

git branch <new-branch-name>

This creates a new section marker pointing at your current page. Besides main, which scrapbookers usually reserve for finalized pages, it is common to make a dev section for work in progress.

Create a branch called dev

Make the dev section:

Type this into the terminal below
git branch dev

See that shiny new green marker called dev stacked on your latest page? That is the section marker you just created. The old main marker is still there too, because both markers point at the exact same page right now.

Branches are just markers that point at pages. Nothing more, nothing less. So for the moment main and dev mark the same page, and that is perfectly normal.

safe Lists or creates branches; nothing at risk.

Two markers, one page

But remember: even though you made dev, you are still actively working on main. The HEAD clip is still on the main marker. If you kept committing now, the new pages would keep going into main.

To start adding pages to the dev section, you need to switch to it first:

git switch <branch-name>

Switch to dev

Move onto the new section:

Type this into the terminal below
git switch dev

Huzzah. HEAD hopped from main to dev. Now you are ready to add a page in the dev section and see what happens.

safe Switches to 'dev'.

A page for dev

You change the file friend.py that Your Friend added, so it says:

print("Hi best friend!")

Since you updated friend.py, that memento has changed. It appears on the table with its pencil badge, and the next two steps are the usual ones: stage it, commit it.

Stage it

Onto the mat.

Type this into the terminal below
git add friend.py
safe 'add' stages files; nothing is discarded.

Commit on dev

Commit it:

Type this into the terminal below
git commit -m "Say hi to a best friend"

Well would you look at that. Notice you did not have to mention dev when making the commit. Because you switched to dev earlier, every new page you commit automatically goes into that section.

As usual, the new page was added at the front. But this time only the dev marker and HEAD moved onto it. main is still sitting on the earlier page. What gives?

safe Creates a new commit; nothing at risk.

The active branch

Since you switched to dev, it became the active section of your scrapbook, and main is no longer active. Three statements that all mean the same thing:

  1. dev is the active branch.
  2. dev is currently checked out.
  3. HEAD points at dev.

Whenever you make a new page, the active branch's marker moves forward to it. Other markers stay put, marking whatever page they were on. So now you have two sections that no longer match: dev is one page ahead of main, but all the earlier pages still belong to both. Trace the arrows back from each marker and they share the same history until... they don't.

Back to main

So what would happen if you switched back to main and added a page there? Let's find out.

Type this into the terminal below
git switch main

HEAD jumps back onto the main marker, and your files change to match main's latest page: friend.py says Hi friend! again on your table. Your edit is safe on the dev page.

safe Switches to 'main'.

A page for main

Now that main is checked out, you are ready to add a page here. You create a new memento called birthday.py with this code inside:

print("It's mah birfday!")

Brand new memento, so it shows up untracked. Stage it, then commit it.

Stage it

To the staging area.

Type this into the terminal below
git add birthday.py
safe 'add' stages files; nothing is discarded.

The storylines split

Commit the new page on main:

Type this into the terminal below
git commit -m "Add birthday.py"

Unbe-LEAF-able. Look what happened to main. It split off from dev. But why?

Because you added different pages to each branch. On dev you changed the memento in friend.py. Then on main you added the brand new memento birthday.py. Trace back from both markers and you will find the same shared parent page. From there, they have diverged into separate storylines.

safe Creates a new commit; nothing at risk.

Two storylines

dev does not know about the latest main page, and main does not know about the latest dev page. Kind of sad if you think about it. But do not shed too many tears: this happens all the time in Git.

Picture it: you are working on a storyline about your birthday party while Your Friend builds one about their holiday trip. You keep your pages in a section called birthday-party, they keep theirs in holiday-trip. Eventually you both want to combine the storylines so the scrapbook has everybody's memories. In Git, combining storylines is called merging, and that is Level 8.

Your turn: back to the section

You want to keep working in the dev section. Which command takes you there?

git-sim
Initial CommitDevlands

The graph appears here when the first command runs. The simulations are drawn by git-sim from a real repository.

The scrapbook · one picture for every Git idea

The scrapbook picture appears as the story is told.