Skip to content
Intermediate GitLesson 3 of 5 1 min read

Branching

Working on something new without touching the code everyone else depends on.

On this page5 sections

A branch is nothing more than a movable pointer to a commit. That simplicity is exactly what makes branching in git fast and cheap, and why it's the foundation almost every git workflow is built on.

HEAD: where you are right now

Git tracks a special pointer called HEAD, which points at whichever branch (and therefore commit) you currently have checked out. When you make a new commit, HEAD - and the branch it points to - both move forward to it.

Why branch at all?

Editing main directly means every half-finished change is visible to, and can break things for, everyone else working from it. A branch gives you an isolated line of development:

main:              A---B---C
                        \
feature/login:           D---E

Commits D and E exist only on feature/login until that branch is merged back - main stays exactly as it was at commit C the whole time.

Creating and switching branches

bash
git branch feature/login       # create a branch (stays on the current one)
git checkout feature/login     # switch to it

# or do both in one step:
git checkout -b feature/login

Listing branches

bash
git branch

Lists every branch in the repository, with an asterisk marking whichever one you currently have checked out.

Try it

Run these in the practice terminal (opens in a new tab). It opens in a new tab, so you keep your place here.
bash
git checkout -b feature/login

When you're ready, hit Mark complete and move to the next lesson.

Challenge

Create a new branch called feature/login and switch to it, in a single command.

Want to experiment first? Try things in the practice terminal (opens in a new tab), then enter the command that solves the task here.

Quiz

Lock in the concepts. Score at least 80% to mark the lesson complete.

0 of 2 answered

Done with this one?

Skipped the challenge? You can mark the lesson complete by hand.

Your notes

Kept on this browser only.