← Git & GitHub from Zero
Branches: git branch, switch, merge and delete

Lesson 3 of 7

Branches: git branch, switch, merge and delete

Work on a copy of your project without touching the original. Create, switch, merge and delete branches, with an essay-draft analogy.

The idea in plain words

Think of writing an essay. Your main draft is the version you are happy to hand in, and that is the main branch. To try a new idea you photocopy the draft and scribble on the copy. That copy is a new branch.

If the idea works, you copy the good changes back into the main draft, which is a merge. If it does not, you throw the copy away and the main draft is untouched.

Commands in this lesson

git branch
List branches. The star marks the one you are on. Add a name to create a branch.
git switch -c <name>
Create a branch and move onto it in one step.
git merge <branch>
Bring another branch into the one you are on.
git branch -d <name>
Delete a branch that has been merged. Git refuses if it has unmerged work.

Written notes from the video

Why branches

What if you could try a risky idea, without any risk to your working project? This is Git and GitHub from zero, episode three: branches. Four commands, and one idea that makes teamwork possible.

The essay analogy

Think of writing an essay. Your main draft is the version you are happy to hand in. That is the main branch. To try a new idea, you photocopy the draft and scribble on the copy. That is a new branch. If the idea works, you copy the good changes back into the main draft. That is a merge. If it does not, you throw the copy away, and the main draft is untouched.

Command 1: git branch

Command one: git branch. On its own, it lists your branches. The star marks the one you are on. Every project starts with one branch, usually called main. Add a name to create a new branch. But that does not move you onto it.

Command 2: git switch -c

Command two: git switch. It moves you to another branch, and your files change to match. Add -c to create a branch and switch to it in one step. This is the form you will use most. Now commit as usual. This commit lives on the new branch only. Switch back to main, and the new file is gone. It is safe on its branch, waiting.

Command 3: git merge

Command three: git merge. Happy with the idea? Switch to the branch you want to update, usually main, and merge the other branch into it. Here main had not changed, so Git simply moves forward. That is called a fast-forward. If both branches changed, Git ties them together with a merge commit instead. And if both changed the same lines, you get a merge conflict. We fix those in episode five.

Command 4: git branch -d

Command four: git branch -d. Once a branch is merged, you do not need the label any more. Delete it to keep things tidy. The commits are not lost. They are part of main now. Only the name goes away. Git protects you. With a small d, it refuses to delete a branch that has unmerged work. A capital D forces it. Use that only when you really want to throw the work away.

Recap

Recap. git branch lists branches. git switch -c creates one and moves you onto it. git merge brings a branch back in. git branch -d tidies up afterwards. A good habit: one branch per task, and keep main working.

Next episode

Next episode: GitHub. You will connect your computer to GitHub, and move commits between them. Subscribe so you do not miss it, and find written notes for every lesson at changecv.com/skills.

Common questions

What is a Git branch?

A branch is a movable label that points at a commit. When you commit while on a branch, the label moves forward, so your experiments stay separate from main until you merge them.

What is the difference between git switch and git checkout?

git switch only changes branches, which makes it easier to understand. git checkout can also restore files, so it does several jobs. For changing branches, git switch is the clearer choice.

What is a fast-forward merge?

If main has not changed since your branch started, Git does not need a merge commit. It simply moves main forward to the branch tip.

Does deleting a branch delete my commits?

Not if the branch was merged. The commits are part of the branch you merged into, and only the label disappears. git branch -d refuses to delete a branch with unmerged work, and git branch -D forces it.

How should I name branches?

Use short, descriptive names, often with a prefix such as feature/login or fix/nav-typo. Work on one task per branch and keep main in a working state.