Sub topic 4
Creating and switching branches
This chapter assumes you have used git init, git add, git commit, git status and git log. It also assumes you are comfortable in a terminal and have Git 2.23 or later, which added git switch. Check your version with git --version.
What creating a branch does
A branch is a small file that holds the ID of one commit. Creating a branch writes a new file like that, and it copies no project files. This is why branching is fast in a repository of any size.
HEAD is a separate reference that normally names the branch you are on. Making a commit moves the branch that HEAD names forward to the new commit. Other branches stay where they are.
Both names share C3 until one of them gets a new commit.
The basic workflow
git branch
git switch -c feature/login
git branch -v
git switch maingit branchwith no arguments lists local branches. The current branch has a*beside it.git switch -c feature/logincreatesfeature/loginat the current commit and switches to it in one step. The-cis short for--create.git branch -vlists each branch with its tip commit ID and subject line. You should seemainandfeature/loginshowing the same commit.git switch mainmovesHEADback tomain. Git updates the files in your working directory to match that commit.
Commits you make on feature/login stay on that branch. After switching back to main, your working directory no longer shows those changes. Switching back to feature/login restores them.
Create without switching
git branch <name> creates a branch and leaves HEAD where it is.
git branch hotfix/typo
git branch -vThis is useful for putting a bookmark on the current commit before you try something risky. It is also useful in scripts that create several branches.
Choosing where the branch starts
By default a new branch starts at the commit you are on. You can give a different starting point as a second argument. It can be a branch, a tag or a commit ID.
git switch -c bugfix/null-check main
git switch -c release/1.0 v1.0.0
git switch -c experiment/old-parser a1b2c3dThe first command branches from main even if you are currently on another branch. The second branches from a tag. The third branches from a specific commit. The ID a1b2c3d is only an example, so use an ID from your own git log --oneline.
The same start-point argument works with git branch, for example git branch bugfix/null-check main.
Switching between branches
| Goal | Command |
|---|---|
| Switch to an existing branch | `git switch main` |
| Create and switch | `git switch -c name` |
| Jump back to the previous branch | `git switch -` |
| Create a branch from a remote branch | `git switch name` (when only `origin/name` exists) |
| Inspect a commit without a branch | `git switch --detach <commit>` |
git switch - is handy when you alternate between two branches. If you have no local branch called name but exactly one remote has name, git switch name creates a local branch that tracks it. Remote-tracking branches are covered in a later chapter.
Switching with uncommitted changes
Switching branches replaces the files in your working directory. Git has three ways of handling changes you have not committed.
- If the target branch has the same version of the files you modified, Git switches and carries your changes along.
- If the target branch differs in a file you modified, Git refuses and lists the files. This prevents data loss.
- Untracked files stay in place unless the target branch tracks a file with the same path. In that case Git refuses.
A refusal looks like this:
error: Your local changes to the following files would be overwritten by checkout:
app.py
Please commit your changes or stash them before you switch branches.
AbortingYou have three safe options:
- Commit the work, even as a rough commit you plan to tidy later.
- Stash it with
git stash push, switch, and restore it later withgit stash pop. - Move the work with you by creating a branch first:
git switch -c new-branch. Creating a branch does not change any files, so it never conflicts with your changes.
The third option is the one to remember when you notice you started editing on main by mistake. Run git switch -c feature/my-work, then commit as usual. main is untouched because you have not committed anything on it.
Resetting a branch name that already exists
git switch -c name fails if name already exists. If you really want to point that name at a new start, use -C, short for --force-create.
git switch -C scratch mainThis moves scratch to the tip of main, discarding the old position of scratch. The old commits are not deleted at once, and the reflog can still find them, but no branch name refers to them any more. Use -C only for throwaway branches or when you know the commits are saved elsewhere.
Common errors
| Message | Cause | Fix |
|---|---|---|
| `fatal: a branch named 'x' already exists` | The name is taken. | Pick another name, switch to it, or use `-C` on purpose. |
| `fatal: invalid reference: x` | The branch or start point does not exist, often from a typo. | Check `git branch -a` for the correct name. |
| `fatal: 'x' is not a valid branch name` | The name has spaces, `..`, `~`, `^`, `:` or a leading `-`. | Use letters, digits, `/`, `-` and `_`. |
| `fatal: not a git repository` | You are outside a repository. | Change into the project directory. |
| Local changes would be overwritten | The target branch differs in a file you edited. | Commit, stash, or branch first. |
| `fatal: invalid reference` on a new repository | No commit exists yet, so no branch exists yet. | Make the first commit. |
A branch is born at a commit, so a repository with no commits has no branches to list or switch to. The initial branch name appears only when you make the first commit.
Older commands you will see
Before Git 2.23, git checkout did both switching and file restoring. Many tutorials and scripts still use it.
| Modern | Older equivalent |
|---|---|
| `git switch main` | `git checkout main` |
| `git switch -c name` | `git checkout -b name` |
| `git switch -` | `git checkout -` |
| `git switch --detach <commit>` | `git checkout <commit>` |
git switch only works with branches, which makes it harder to overwrite files by accident. Both commands work, so you can read old instructions and translate them.
Checking your state
Run these before and after switching until the pattern feels natural:
git statusshows the current branch on its first line.git branch --show-currentprints only the current branch name. It prints nothing whenHEADis detached.git log --oneline --decorate -5shows which branch names sit on which commits, includingHEAD -> branch.
Exercise
You are on main and have edited app.py without committing. You realize the work belongs on a new branch called feature/search. Which command moves the work to the new branch without losing it, and why does it not fail with a "would be overwritten" error?
Show answer
Run git switch -c feature/search. Creating a branch only writes a new pointer at the current commit, and the new branch has the same files as main, so nothing in your working directory conflicts. Your uncommitted edits come along, and you can commit them on feature/search, leaving main unchanged.
Exercise
You run git switch -c release/2.0 v2.0.0 and then git branch -v. Which commit does release/2.0 point to, and which branch does HEAD name?
Show answer
release/2.0 points to the commit that the tag v2.0.0 refers to, not to the commit you were on before. HEAD names release/2.0, because git switch -c switches to the new branch after creating it.