Multiple Branches — Git

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…

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

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'

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

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:

$ 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

⚠️ Wrong branch, wrong name

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.

What you will learn