Great work!

XP to next level

BugEater

Creating and Switching: git branch, git checkout, git switch

Learning Objectives

By the end of this lesson you will be able to:

  • Create a branch and switch to it, both separately and in one command
  • Choose correctly between git checkout and the newer git switch
  • Understand what happens to your uncommitted changes when you switch
  • Recover cleanly when Git refuses to switch branches

Two Commands, One Job

Historically Git used git checkout for a startling number of unrelated things: switching branches, restoring files, detaching HEAD, creating branches. That overloading caused real accidents — git checkout somefile silently destroyed uncommitted work.

Git 2.23 split the job in two:

Task Modern command Older command
Switch branches git switch git checkout
Restore file contents git restore git checkout --

git checkout still works and always will. You will see it in every tutorial written before 2019 and in most team documentation today. Learn both; prefer switch when you have the choice, because it can only ever do one thing.

Creating a Branch

git branch with a name creates the branch and does not move you onto it:

git branch feature/auth-test

You are still exactly where you were. The new branch points at the same commit you are on.

To create it and move onto it — which is what you want 95% of the time:

git switch -c feature/auth-test        # -c for "create"
Switched to a new branch 'feature/auth-test'

Or the classic form, which does the same thing:

git checkout -b feature/auth-test      # -b for "branch"

Switching Between Existing Branches

git switch main
git switch feature/auth-test

And a genuinely useful shortcut — - means "the branch I was on before", exactly like cd -:

git switch -

Bouncing between main and your test branch to compare behaviour is a constant activity in QA. This shortcut is worth the ten seconds it takes to memorise.

Branching from Somewhere Other Than Here

By default a new branch starts at your current commit. You can start it anywhere:

git switch -c test/rc-verification v2.4.0        # from a tag
git switch -c fix/regression 8f3c2a1             # from a specific commit
git switch -c qa/dev-copy origin/develop         # from a remote branch

That first one is a genuine QA workflow: branch from the exact release tag, reproduce the customer's problem on the exact code they are running.

What Happens to Uncommitted Changes

This is the part that surprises people, so let's be exact.

Your uncommitted changes come with you. Git does not stash them, revert them, or leave them behind. If you have edited checklist.md and you switch branches, the edited checklist.md is still edited on the other branch.

That is usually convenient and occasionally alarming. It works as long as the change doesn't collide with the branch you're moving to.

When it does collide, Git refuses:

error: Your local changes to the following files would be overwritten by checkout:
        config/test-env.yaml
Please commit your changes or stash them before you switch branches.
Aborting

This is Git protecting you, not failing. Nothing was lost and nothing was changed. You have three honest options:

  1. Commit the work — if it belongs on the branch you're currently on.
  2. Stash itgit stash push -m "wip", covered properly in Module 4.
  3. Discard itgit restore config/test-env.yaml, if you genuinely don't want it.

There is no fourth option that magically switches anyway, and looking for one is how people lose work.

A Realistic Sequence

git switch main                        # start from a known state
git pull                               # make sure it's current
git switch -c test/PAY-4417-verify     # branch for this ticket's testing
# ... break things, add logging, run the suite ...
git switch -                           # jump back to main to compare
git switch -                           # and back again

Pro Tip: Always branch from an up-to-date main. git switch main && git pull && git switch -c <new> is three commands and it prevents the single most common "why is my branch missing that fix?" conversation.

Key Takeaways

  • git branch <name> creates without switching; git switch -c <name> creates and switches
  • git checkout -b <name> is the older equivalent of git switch -c and is still everywhere
  • git switch - returns to the previous branch — invaluable when comparing behaviour
  • You can branch from a tag, a commit or a remote branch, not just from where you are
  • Uncommitted changes travel with you; if they would be overwritten, Git aborts safely
  • Commit, stash or discard — those are the only three real answers to a blocked switch

Quiz

What does git branch feature/auth-test do?

Why was git switch introduced when git checkout already existed?

You have uncommitted edits and run git switch main. What happens?

A customer reports a bug on release v2.4.0. Which command starts a test branch from exactly that code?