Basic

Why you never work directly on main

A branch is a parallel copy of your repository. Work on it without touching the main branch, and when it's done you bring it back in. It's the whole reason collaboration is possible: anyone can experiment freely, knowing the main line stays safe.

What you will see

Deleted branch feature/login (was def4567)

About 2 min of reading

Try It

I wanted to add a login page and nearly edited the live site directly, then remembered branches exist. Let me split main into a parallel copy, work on it, and bring the work back home so main never sees half-finished code.

1

Start from a clean main with one commit. On this branch the project has just its first version. I am about to leave main untouched and do my experiment somewhere else, so I make sure main has a real commit to build a branch from.

echo "landing page" > index.html git add index.html git commit -m "Add landing page"

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

What it prints
[main abc1234] Add landing page 1 file changed, 1 insertion(+)
This creates the initial commit. The branch it lands on is usually main, but check with git branch --show-current if your repo names it differently.
2

Split main into a parallel copy with git switch -c. The new flag creates the branch and moves me onto it in one step. My working copy now shows exactly main, because the branch starts from the same commit, the live site has not changed a single line.

git switch -c 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"
The -c flag is what creates the branch. Without it, git switch would just move an existing branch, and there would be nothing new to work on.
3

Make my feature change here, on the branch, nowhere near main. Because I am on feature/login, this commit lands on the feature branch only. The main branch still has exactly one commit and its own history, untouched.

echo "login form" >> index.html git add index.html git commit -m "Add login form to landing page"

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

What it prints
[feature/login def4567] Add login form to landing page 1 file changed, 1 insertion(+)
Notice the branch name in brackets changes to feature/login. That is proof the commit landed on my branch, not on main.
4

git branch lists every branch I have, and the one with an asterisk is the one I am standing on. This is the check I do before I forget which copy I am working in. feature/login has the asterisk, main has just one commit.

git branch

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

What it prints
main * feature/login
The asterisk means "you are here". If it is on the wrong branch, do a git switch back before you commit anything else.
5

Bring the work back home. Switch back to main first, then merge the feature branch in. Git copies my new commit onto main, so main now has the login page and the whole feature history.

git switch main git merge feature/login

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

What it prints
Switched to branch 'main' Updating abc..def Fast-forward index.html | 1 insertion(+)
Merge only happens once both branches are on main. If main has moved on since the branch was made, git will do a real merge with markers instead of a fast-forward.
6

The feature branch has done its job. Delete it to keep things tidy, main now has everything it shipped, so the branch is only clutter.

git branch -d feature/login

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

What it prints
Deleted branch feature/login (was def4567)
A soft -d refuses to delete a branch that still has commits main does not have. If that happens, the work is not actually merged and something went wrong.

Put it in order

Before you write anything, put these lines back in the right order. Click a line to move it up one step. This support step is easier than writing code from scratch, and it is here to make the last example stick.

Data Flow

Copy main into a new branch, work there, then merge it back when it's ready.
1 Create a branch from main
↓
2 Do the work there
↓
3 Finish the feature
↓
4 Merge it back to main
Why this works
Keep main clean

The main branch is the working version of the project. Always do your experimentation on a separate branch, so a mistake mid-way never leaves main broken.

Many branches at once

Different people can have their own branches at the same time. Each works alone, then their work is joined back in later. That's how a team can move fast without colliding.

Examples

Branched off before it was ready

You: "I want to build a login page without breaking the live site."
git switch -c feature/login

main:           the live site
feature/login:  your new work, kept separate

While you build on feature/login, the live site keeps working exactly as before. Nothing you do on the branch can affect it until you deliberately merge. That separation is worth a lot when the code you're writing breaks something.

Switching between your branches

git switch feature/login
git branch

git switch moves you to another branch. The files on your screen change instantly to match. git branch lists every branch you have, and the one with an asterisk is the one you're currently on.

Switching branches never loses work, it just points you at a different snapshot. Go back to main whenever the feature is finished and ready to ship.

Bringing a branch back home

git switch main
git merge feature/login

When the feature works and looks good, switch back to main and merge. Git copies your new work onto the main branch. After that, main has the login page, and the feature branch has done its job, it can be removed to keep things tidy.

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.

← What a commit actually does to your history
Previous Lesson
What a commit actually does to your history
Next Lesson
A pull request is just a conversation
A pull request is just a conversation →