TetSharing
Git & CLI tutorial · 12 chapters
  1. Terminal basics
  2. Working with files
  3. Pipes and redirection
  4. What Git is
  5. Staging and commits
  6. Viewing history
  7. Comparing changes
  8. Branches
  9. Merging
  10. Merge conflicts
  11. Undoing changes
  12. Remotes, push and pull

Final Git & CLI quiz
Cheat sheet: git commands
Cheat sheet: linux shell commands

Chapter 8 of 12 · By Ethan Cole · Updated November 21, 2026

Git Branches: Create, Switch and List Branches

A branch is an independent line of work. You create one to build a feature or fix a bug, commit freely on it, and the main branch stays untouched until you decide to merge. Teams use branches so several people can work on different things at once.

What a branch really is

In Git a branch is just a movable label that points to a commit. Creating one is instant and costs almost nothing, no files are copied. When you commit on a branch, the label moves forward to the new commit. HEAD points to the branch you are on, which is how Git knows which label to move.

Creating and switching

  • git branch lists branches; the current one is marked with *.
  • git switch -c feature-search creates a branch and switches to it in one step.
  • git switch main goes back to main.
  • git branch -d feature-search deletes a branch that has been merged. Git refuses if it has unmerged commits; -D forces it.

Older tutorials use git checkout -b name and git checkout name. They still work, but git switch was added to Git specifically for changing branches and is harder to misuse, since checkout also restores files.

Files change when you switch

Switching branches updates the files in your working directory to match that branch. In the example, a file created and committed on feature disappears from the folder when we switch back to main, and comes back when we return. Nothing is lost: it lives in the feature branch's commits.

Terminal · bash + git · copy and run locally
git init -q
echo "Home page" > index.txt
git add index.txt
git commit -q -m "Add home page"
git switch -c feature
echo "Search box" > search.txt
git add search.txt
git commit -q -m "Add search"
ls
git switch main
ls
git branch
git log --oneline --all

Transcript (verified by our test suite in a fresh temp folder)

$ git init -q
$ echo "Home page" > index.txt
$ git add index.txt
$ git commit -q -m "Add home page"
$ git switch -c feature
Switched to a new branch 'feature'
$ echo "Search box" > search.txt
$ git add search.txt
$ git commit -q -m "Add search"
$ ls
index.txt
search.txt
$ git switch main
Switched to branch 'main'
$ ls
index.txt
$ git branch
  feature
* main
$ git log --oneline --all
88d79c2 Add search
64fbb15 Add home page

The final git log --oneline --all lists commits from every branch, and the branch names in parentheses show where each label points.

Uncommitted work

If you have uncommitted changes that would be overwritten, Git refuses to switch and tells you which files are in the way. Commit them, or set them aside with git stash and bring them back later with git stash pop.

Naming branches

Short, descriptive names in lowercase with hyphens work well: fix-login-redirect, add-dark-mode. Many teams add a prefix such as feature/ or bugfix/. Avoid spaces, which Git does not allow.

Summary

  • A branch is a lightweight pointer to a commit; HEAD marks the current one.
  • git switch -c name creates and switches; git switch name moves.
  • Switching updates your files; commit or stash first.

Quiz: Branches

1. What does git switch -c login-page do?

-c creates the branch before switching to it.

2. How does Git mark the current branch in git branch output?

The current branch is prefixed with an asterisk.

3. A file committed only on feature disappears after git switch main. Why?

The file still exists in feature's commits and reappears when you switch back.

Official docs

Written under the pen name Ethan Cole with AI-assisted drafting. Every code example on this page is executed by our test suite before publishing. Found a mistake? Tell us.