Creating and Switching Branches
Creating and switching branches are the two most common branch operations. Once you have a branch, you switch to it, make commits, and those commits belong to that branch — main and every other branch stay exactly where they were.
Creating a branch
Section titled “Creating a branch”git branch feature/tour-descriptionsThis creates a new branch pointing at your current commit. It does not switch you to the new branch — you stay on whatever branch you are currently on. To confirm:
git branchOutput:
feature/tour-descriptions* mainThe * marks your current branch. You are still on main. The new branch exists, but HEAD has not moved.
Switching to a branch
Section titled “Switching to a branch”git switch feature/tour-descriptionsOutput:
Switched to branch 'feature/tour-descriptions'Now HEAD points at feature/tour-descriptions. Any commits you make will advance this branch, not main.
Run git log --oneline --graph --all to see both branches pointing at the same commit:
* b7e9d03 (HEAD -> feature/tour-descriptions, main) Add difficulty and gear notes* a3f8c12 Add initial tour notesCreating and switching in one step
Section titled “Creating and switching in one step”The most common workflow combines both steps:
git switch -c feature/tour-descriptions-c means “create.” This is the command you will use almost every time — create the branch and switch to it immediately.
Making commits on a branch
Section titled “Making commits on a branch”On feature/tour-descriptions, add content to a file:
echo "Best views from the ridge at mile 3" >> pine-ridge.txtgit add pine-ridge.txtgit commit -m "Add viewpoint note to Pine Ridge description"Now check the graph:
git log --oneline --graph --all* c2a1d08 (HEAD -> feature/tour-descriptions) Add viewpoint note to Pine Ridge description* b7e9d03 (main) Add difficulty and gear notes* a3f8c12 Add initial tour notesfeature/tour-descriptions has moved forward with the new commit. main is still at b7e9d03. The branches have diverged.
Switching back to main
Section titled “Switching back to main”git switch mainWhen you switch branches, Git updates your working tree to match the state of the branch you switched to. Open pine-ridge.txt — the line you added on the feature branch is gone. It is not deleted; it lives on feature/tour-descriptions. Switch back and it reappears.
This is one of the things that surprises people the first time: your files actually change when you switch branches. Git is updating your working tree to reflect the state of the commit that branch points to.
What switching requires: a clean working tree
Section titled “What switching requires: a clean working tree”Git will not let you switch branches if you have uncommitted changes that would be overwritten by the switch. If you have modified files, Git will warn you:
error: Your local changes to the following files would be overwritten by checkout: pine-ridge.txtPlease commit your changes or stash them before you switch branches.AbortingYour options:
- Commit the changes on the current branch before switching
- Stash them temporarily (
git stash, covered in Module 04)
This protection prevents you from accidentally losing work.
Listing all branches
Section titled “Listing all branches”git branch # local branches onlygit branch -a # local and remote branchesThe * marks your current branch.
Older syntax: git checkout
Section titled “Older syntax: git checkout”Before git switch existed (added in Git 2.23, 2019), the command was git checkout:
git checkout feature/tour-descriptions # switchgit checkout -b feature/tour-descriptions # create and switchBoth still work. You will see checkout in older documentation, Stack Overflow answers, and tutorials. git switch is the modern preferred form for branch operations.
Exercise
Section titled “Exercise”- In your
git-practicefolder, create a new branch and switch to it in one step:
git switch -c feature/add-river-canyon- Add content to
river-canyon.txtand make a commit on this branch:
echo "River Canyon Trail: canyon rim views and waterfall stops" > river-canyon.txtgit add river-canyon.txtgit commit -m "Add River Canyon trail description"-
Run
git log --oneline --graph --alland observe thatfeature/add-river-canyonhas advanced whilemainhas not. -
Switch back to
main:
git switch main-
Open your working tree and confirm that
river-canyon.txthas no content (or does not exist) — the feature branch work is not visible frommain. -
Run
git log --oneline --graph --allagain frommainand confirm both branches are visible.
git branch <name>creates a branch at the current commit without switching.git switch <name>switches to an existing branch.git switch -c <name>creates and switches to a new branch in one step — the form you will use most.- Switching branches updates your working tree to reflect the state of that branch’s latest commit.
- Git will not let you switch with uncommitted changes that would be overwritten.
git branchlists local branches;git branch -aincludes remotes.
Next: merging — how to bring the commits from a feature branch back into main.