Basic

Working in a parallel universe

Branches are the only part of git that feels like cheating. You can break things in a copy and never once touch the real project.

What you will see

Updating abc1234..abc5678 Fast-forward login.py | 1 insertion(+), 1 deletion(-)

About 2 min of reading

Try It

A branch lets you break things in a copy that shares nothing with your real project. Let me split one off, break it on purpose, and bring it back.

1

Start from the commit I already made on main, and make sure of it. git branch with no arguments lists every branch I have and marks the current one with an asterisk. Right now main is the only one there.

git branch

Before you run it, say out loud what it will print. Then open the answer.

What it prints
* main
The asterisk means "you are here." If it is not on main, `git checkout main` first, everything below starts from that commit.
2

Split off and switch in one move. -b means create, and the name after it is the branch. My folder now shows the same files as before, but any commit from here goes to the new branch, not main.

git checkout -b feature-login

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Switched to a new branch "feature-login"
3

Now break something on purpose. Edit the finished file, stage it, commit it. The change lives entirely on feature-login. main has not moved an inch, the real work is untouched.

echo "hardcoded password" > login.py git add login.py git commit -m "break login"

Before you run it, say out loud what it will print. Then open the answer.

What it prints
[feature-login abc5678] break login 1 file changed, 1 insertion(+), 1 deletion(-)
The success line says feature-login, not main. That is the whole point. If it says main, I forgot to switch off of feature-login first.
4

Confirm the mess is mine and main is clean. git log --oneline shows only my broken commit on this branch. Switch back to main and run the same command, my broken commit is nowhere to be seen.

git log --oneline git checkout main git log --oneline

Before you run it, say out loud what it will print. Then open the answer.

What it prints
abc5678 break login abc1234 Add login abc1234 Add login
After checkout main, the log should show only Add login, not break login. The break stays isolated on the other branch, exactly as branches are supposed to.
5

Time to bring the real work back. Merge the branch into main. Git stitches the two histories together on top of main's latest commit. Since I only touched login.py on one side, there is nothing to fight over, the merge just works.

git checkout main git merge feature-login

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Updating abc1234..abc5678 Fast-forward login.py | 1 insertion(+), 1 deletion(-)
Fast-forward means git could simply move main onto my commit with no conflict. If both branches touched the same lines, you would get a conflict and stop reading for the next lesson.

Data Flow

A branch splits off from main, you commit on it in safety, then merge it back.
1 Create a branch off main
↓
2 Commit your work on the branch
↓
3 Switch back to main
↓
4 Merge the branch into main
↓
5 The two histories combine
Why this works
A branch is just a pointer

There is no magic box of branches. A branch is a small label that points at a commit. main just points at your latest commit on the main line, and feature-login points at the latest commit you made on that branch. Creating or moving a branch only moves the label, the commits already exist.

When you have a choice

Merge vs rebase (you'll meet both)

Two ways eventually join branches:

git merge branch     # adds a merge commit, keeps both histories
git rebase branch    # replays your commits on top, straight line

For now merge is the safe default. rebase is a sharpening tool you'll learn in the Advanced lessons, it looks cleaner but rewrites history, so it is not for work you have already shared.

Examples

Split off without risk

git checkout -b feature-login

-b means create a new branch named feature-login and switch to it in one move. Your folder now shows the same files as before, but anything you commit from here goes to the new branch, not the main one. Make a mess? No problem, the real work is safe on main.

The moment I forgot to switch back

git checkout main

I built a whole feature, committed it happily, then tried to push from main and found my work missing. I had simply forgotten to switch back off feature-login. Switching back is the command above. Git moves your working files to match whatever branch you are on, so this also undoes the confusing feeling before it starts.

Folding one branch into another

git checkout main
git merge feature-login

Once the feature is done and tested, merge it back into main. Git stitches the two histories together. The awkward part happens only when both branches touched the same lines, then git stops and asks you, which is where the next lesson starts.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← Choosing what to save
Previous Lesson
Choosing what to save
Next Lesson
Before I press enter
Before I press enter →