0%

Branches · practice

Start a Separate Line of Work

You want to add opening hours to the neighborhood guide, but you do not want the unfinished change to move main. A separate gives that work its own movable name while it is in progress.

Create the branch and switch to it

git switch changes which branch is current. The option -c means create: Git creates the branch named after the option and immediately switches to it.

Run this command exactly once:

git switch -c add-opening-hours

Read the command from left to right: ask Git to switch, create a branch as part of that switch, and name the branch add-opening-hours. Git starts the new branch at the you were already on, so no file needs to change and no commit is made.

If Git says the branch already exists, you probably ran the command earlier. Do not create another branch. Use git switch add-opening-hours to switch to the branch that is already there; without -c, git switch switches to an existing branch.

Now ask for the branch list again:

git branch

The list now includes add-opening-hours, and its line has the *. HEAD points to this new current branch. At this moment add-opening-hours, main, and clearer-title all point to the same baseline commit.

Clarify the guide title       <- main, clearer-title, add-opening-hours (HEAD)
        |
Start the neighborhood guide

Creating the pointer was cheap because Git did not copy the working tree. The branches will become meaningfully different when one of their pointers moves to a new commit.

What did git switch -c add-opening-hours do?

Notice how little happened. No file changed, no commit was made, and the project on disk is identical to what it was a minute ago. All you did was add a third name pointing at the commit two other names already pointed at. That is why experienced people make branches without hesitating: there is almost nothing to undo if the idea turns out badly.

In the next lesson you will make a commit, and only one of those three names will move.