Your website already works. Now you want to try a new idea for the navbar (the menu at the top of the page), and that idea might break everything. If you edit right away and the idea fails, you have to undo the changes one by one. There's a calmer way.
A branch is a separate line of work in your repo: you can try a new feature without disturbing the version that already works. It's like save slots in a game. Your save in the "main" slot has reached level 3, and you want to try a risky path. You make a new slot from the same point and keep playing there. The "main" slot stays at level 3, and if the experiment goes wrong, you just go back to it. In Git, each slot is one branch, and the last save in a slot is the newest commit on that branch.
A branch is a name that points at one commit
You already know each commit is one snapshot of your project. Inside Git, a branch is just a name that points at one commit: the newest commit on that line of work. Making a branch copies no files at all, so it's always instant, however big the project is.
mainis the main branch, which Git creates with your first commit. In older repos it's often calledmaster. Before the first commit,maindoesn't really exist yet, because there's no commit for it to point at.HEADis the "you are here" marker. HEAD points at one branch, and that branch is called the active branch. Your next commit goes onto the active branch.
Make a branch and switch to it
There are two ways:
# Way 1: two steps
git branch feature-navbar # make a new branch, you're still on main
git switch feature-navbar # move to feature-navbar
# Way 2: one step, make it and move to it at once
git switch -c feature-navbar
git branch feature-navbaronly makes a new name at the commit you're on. You haven't moved.git switch feature-navbarmoves HEAD to that branch. The files in your working folder are replaced with the versions in the commit that branch points at.git switch -c feature-navbarmakes the branch and moves to it at the same time.-cis an option, short for create.git checkoutis the older command that still works everywhere.git checkout feature-navbardoes the same asgit switch feature-navbar, andgit checkout -b feature-navbardoes the same asgit switch -c feature-navbar.switcharrived in Git 2.23 just for moving between branches, so it's clearer. You'll meet both.
Here's what happens in the terminal, in a repo whose index.html holds <nav>Home</nav> and which already has its first commit:
$ git branch
* main
$ git switch -c feature-navbar
Switched to a new branch 'feature-navbar'
$ git branch
* feature-navbar
main
git branch with no name lists every local branch (the branches on your computer), sorted alphabetically, with a * in front of the active branch. Say there are main, feature-a and feature-b, and you're on main. It prints feature-a, feature-b, then * main.
A commit only moves the active branch
Let's continue with that repo. On feature-navbar, you add a line to index.html (echo "<a>Products</a>" >> index.html), run git add index.html, then commit. In the table, C1 is the first commit, and C2 is this new commit. Before you read the table, guess: when C2 is made, does main move forward too, or stay where it is?
| Step | Command | main at |
feature-navbar at |
HEAD |
|---|---|---|---|---|
| 1 | git commit -m "initial" |
C1 | not yet | main |
| 2 | git switch -c feature-navbar |
C1 | C1 | feature-navbar |
| 3 | git commit -m "add Products link" |
C1 | C2 | feature-navbar |
| 4 | git switch main |
C1 | C2 | main |
- Step 2: the new branch is made on the same commit as
main. No new commit yet. - Step 3: commit C2 is made, and only the branch HEAD points at,
feature-navbar, moves to C2.mainstays on C1. - Step 4: you go back to
main. Git replaces the files in your working folder with the C1 snapshot, soindex.htmlholds just<nav>Home</nav>again. The Products link isn't lost: it's safe in C2, waiting onfeature-navbar.
That's the point of a branch: main stays the version that works, and your experiment grows on its own line until it's ready to be joined. How to join it is in the Merging lesson.
⚠️ Three ways making a branch goes wrong
- Making a branch before the first commit. A branch has to point at a commit. In a repo with no commits, even
maindoesn't exist yet, and in the exercisemainmust exist. Make a commit first, then make the branch. - Thinking
git branch feature-navbarmoved you. That command only makes a name. Your next commit still goes ontomain. Check first withgit branch(look for the*) orgit status(theOn branch ...line). - Spaces or typos in the branch name. A branch name can't contain spaces, so
git branch feature navbardoesn't makefeature-navbar. Use a hyphen and lowercase letters, exactly as the exercise asks.
TL;DR: A branch is a separate line of work. In Git, a branch is just a name that points at one commit, and HEAD (the "you are here" marker) points at the active branch, where the next commit goes. git branch lists the branches (alphabetical, * = active branch). git branch name makes a branch without moving. git switch name (or git checkout name) moves to that branch. git switch -c name (or git checkout -b name) makes a branch and moves to it at the same time. A commit only moves the active branch, and the others stay in place. The main branch is usually main (master in older repos), and it only exists after the first commit.
⚡ A branch is a separate line of work
Meaning: A branch is a separate line of work, for trying an idea without disturbing the version that works. In Git, a branch is just a name that points at one commit, the newest commit on that line. Making a branch copies no files, so it's always instant. The main branch is main, and it only exists after the first commit.
⚡ Make it and move to it
Write:
git switch -c feature-navbar
Meaning: git switch moves you to a branch. The option -c (short for create) makes the branch first, so this command makes feature-navbar and moves you to it at the same time. The older command is git checkout -b feature-navbar. With just git branch feature-navbar, the branch is made but you haven't moved.
⚡ A commit only moves the active branch
Guess first: you make a commit on feature-navbar. Does main move forward too?
No. Only the branch that HEAD (the "you are here" marker) points at moves forward, and that is feature-navbar. main stays on its old commit, and when you run git switch main, your folder goes back to that commit's snapshot.
Meaning: A new commit only moves the active branch, the branch HEAD points at. The other branches stay in place. Your change isn't lost: it's safe on feature-navbar, waiting to be joined into main.
⚡ Check where you are
Write:
git branch
Meaning: git branch lists every local branch (the branches on your computer), in alphabetical order, with a * in front of the active branch, the branch you're on.