learn git.
0/25 done
learn git/branches/course 16 of 25

git switch: move between universes

Your whole folder changes to match the branch you stand on.

9 min read·3 sections·3 tasks
the key idea

git switch rewrites your working folder to match another branch — files appear, vanish, and change to match that universe.

01The mind-bending part, explained

When you run git switch feature, your files change. Files created on feature appear; files deleted there vanish; edits made there are suddenly in place. Nothing is lost — every version of every file is safe in the album — but your working folder is rewritten to be feature's universe.

It feels like teleportation. It is really just git materialising the photograph that feature's label points at. One folder, many universes, and the switch command decides which one you are standing in.

One exception travels with you: untracked files — files no photograph has ever included — come along across switches. Git treats them as none of the universes' business, so they just sit there while the tracked world shifts around them.

[bash]· over there, and back
git switch feature
git switch main
The star tells you where you are

After any switch, git branch (or the prompt in many terminals) shows the star on your new branch. When confused about where you are, that star is the ground truth.

02Create and switch in one move

Two commands are typed together so often that git combined them: git switch -c feature creates the branch and steps onto it in one go. The -c means create. In real work you will use this constantly — new idea, new branch, one command, start working.

You will also constantly see an older spelling: git checkout feature does the same move, and git checkout -b feature is the old create-and-switch. Checkout is the grand old command that does many jobs; switch was added to do exactly this one job with an impossible-to-confuse name. Both work everywhere. This simulator understands both.

modernoldmeaning
git switch featuregit checkout featuremove to an existing branch
git switch -c featuregit checkout -b featurecreate and move
[bash]· make it, then stand on it
git switch -c fix-typo

03Git protects you from losing work

If you have edited a tracked file and not committed it, switching branches could overwrite that edit — so git refuses: error: Your local changes would be overwritten by checkout, plus the name of the file, plus Aborting. Nothing happened; your work is intact; git just declined to gamble with it.

The fix is always the same and always boring: commit your work first, then switch. A commit takes ten seconds and makes the switch safe. Git blocking you here is not an error — it is git being the friend who takes your drink away before you text your ex.

Switching with dirty hands

Tracked files must be committed (or restored) before a switch. Untracked files travel freely. If git refuses your switch, read the file names it prints — that is exactly what it is worried about.

>>

Practice

A feature branch already exists. Go and work there.

[practice]· a real terminal, safely simulated
practice terminal — everything here is simulated, nothing can break
type 'help' to see the commands this terminal understands
~/my-project (main) $
Your tasks1/3 done
  1. 01.Switch to the feature branch.

    hint: Try: git switch feature

  2. 02.Create feature.txt, stage it, and commit it — on feature.

    hint: echo "made on feature" > feature.txt then git add . and commit

  3. 03.Switch back to main — feature.txt should be gone.