In a real project you rarely have just one branch. The login feature is built on one branch, search on another, and they all start from main. Think of one desk and a few binders. The desk is your project folder, each binder is a branch, and the papers inside are files. Only one binder can be open on the desk at a time: the branch HEAD points to. Switching branches means closing one binder and opening another, so the papers on the desk are swapped for that binder's contents. Loose papers you haven't put in a binder yet (changes that aren't committed) are carried along, unless the binder you're opening has its own version of the same paper. Then Git stops, so your paper doesn't get overwritten.
One branch per piece of work
This example starts from a repo with one file, app.js, then makes three branches from main:
$ git init
$ git add .
$ git commit -m "initial"
$ git branch feature-a
$ git branch feature-b
$ git branch feature-c
$ git branch
feature-a
feature-b
feature-c
* main
git branch namemakes a new branch on the commit HEAD is on, but you don't move to it. That's why the*is still onmain.- All four branches point to the same commit, "initial". A new branch is just a new label, no files get copied.
git branchsorts the names alphabetically, somainappears last.
Switching branches: git switch and git checkout
There are two ways to switch branches with git switch:
$ git switch feature-a # move to a branch that already exists
Switched to branch 'feature-a'
$ git switch -c fix-navbar # make a new branch, then move onto it
Switched to a new branch 'fix-navbar'
git switch namemoves to an existing branch.git switch -c name(c for create, the long form is--create) makes the branch and moves to it with one command.- The older commands are
git checkout nameandgit checkout -b name, with the same result.switchhas existed since Git 2.23 and only changes branches.checkoutis also used for other things, like restoring a file's contents, so it's easier to misuse. Both still work. - Switching branches replaces the files in your project folder with the contents of the latest commit on the branch you move to.
Taking turns on several branches
In this example, you make one commit on feature-a, then one commit on feature-b. echo "..." >> app.js adds one line at the end of app.js:
$ git switch feature-a
$ echo "login()" >> app.js
$ git add .
$ git commit -m "add login"
$ git switch feature-b
$ echo "search()" >> app.js
$ git add .
$ git commit -m "add search"
$ git switch main
This table shows where each label is after each step:
| After | main |
feature-a |
feature-b |
HEAD |
|---|---|---|---|---|
three git branch |
initial | initial | initial | main |
| commit "add login" | initial | add login | initial | feature-a |
| commit "add search" | initial | add login | add search | feature-b |
git switch main |
initial | add login | add search | main |
The picture below shows the end result. Each o is one commit, and <- points at the branch labels on that commit:
o add login <- feature-a
/
o initial <- main, feature-c
\
o add search <- feature-b
- A commit only moves the branch you're on.
mainstays on "initial", so each feature can be tested and merged on its own. - When you switch to
feature-b, the linelogin()disappears fromapp.js. It isn't really gone: it's safe in the commit "add login", and it appears again when you switch tofeature-a.
Switching with changes that aren't committed
You're on feature-b and you've changed app.js, but haven't committed. On feature-a, app.js is different. If you now run git switch feature-a, is your change lost, is it carried along, or is the switch refused? Take a guess.
One thing is certain: Git never silently deletes changes you haven't committed. There are two outcomes:
- They are carried along. If the file you changed is the same on both branches, your change is carried to the branch you move to, still not committed.
- Refused. If that file is different on the branch you're moving to, switching would overwrite your change, so Git stops:
$ git switch feature-a
error: Your local changes to the following files would be overwritten by checkout:
app.js
Please commit your changes or stash them before you switch branches.
Aborting
So your change isn't lost. Because app.js is different on feature-a, Git refuses. You have two options: commit first, or store it for later with git stash. git stash keeps changes you haven't committed in a separate place, and git stash pop puts them back:
$ git stash # put the changes aside, the folder is clean again
$ git switch feature-a
$ git switch feature-b
$ git stash pop # bring back the changes you put aside
git stash is covered fully in the Git Stash & Gitignore lesson.
Tidy branch names
feature/feature-namefor a new feature, for examplefeature/login.fix/bug-namefor a bug fix, for examplefix/login-button.hotfix/namefor an urgent fix to the app people are using right now.- Use lowercase letters and hyphens. Spaces aren't allowed in branch names.
- Start each new branch from the latest
main, and merge as often as you can so conflicts stay small.
⚠️ Wrong branch, wrong name
- Forgetting to go back to
main.git branch namemakes the branch where HEAD is. If you're onfeature-aand it already has a commit, the new branch carries that commit too. The exercise also checks that you end onmain. - Typos in the name.
feature_aorfeatur-ais a different branch, and the check in the exercise looks for the exact name. - Committing on the wrong branch. Before you commit, check the first line of
git status("On branch ...") or the*ingit branch. - Panicking when a switch is refused. The "would be overwritten" message is protecting your change. Commit or
git stashfirst, then switch again.
TL;DR: Each piece of work gets its own branch that starts from main. git branch name makes a branch without moving. git switch name moves to that branch, and git switch -c name makes a branch and moves to it (the older forms are git checkout name and git checkout -b name). A commit only moves the branch you're on. When you switch branches, changes you haven't committed are carried along if the file is the same on both branches. If that file differs, Git refuses the switch: commit first, or store the changes for later with git stash, and later restore them with git stash pop. Give branches clear names, like feature/login or fix/login-button.
⚡ One branch per piece of work
Write:
git branch feature-a
git branch feature-b
git branch feature-c
Meaning: git branch name makes a new branch on the commit you're on, without moving to it. If you're on main, these three branches start from main and point at the same commit as main.
The exercise checks for the exact branch names and that you end on main.
⚡ Switch branches, the folder changes too
Write:
git switch feature-a
Meaning: git switch moves to another branch. The project folder's content is replaced with the latest commit on the branch you move to. A line committed only on the branch you leave disappears from the folder, but it is safe in its commit: it appears again when you switch back to that branch.
⚡ Changes you haven't committed
Guess first: you've changed app.js but haven't committed, then you run git switch feature-a. Are your changes lost?
No. If that file is the same on your current branch and on feature-a, your changes are carried along. If it differs, Git refuses the switch with a would be overwritten message.
Meaning: Git never silently deletes changes you haven't committed. If the switch is refused, commit first, or store the changes for later with git stash.