Switching branches
git switch moves you to another branch: it points HEAD at that branch and replaces the files in your working tree with the ones from its commit.
Last updated: 12 Oct, 2026 · git 2.54
Creating a branch leaves you where you are. Switching is the step that makes the branch real: your folder changes to what that branch holds, your next commit lands on it, and the branch you came from is left exactly as it was.
Syntax: git switch arrived in Git 2.23 for exactly this job. git checkout still does it and is what you will see in older material.
git switch <name> # move to an existing branch
git switch -c <name> # create it and move to it in one step
git switch - # back to the branch you were on before
git checkout <name> # the older command, same moveView the code here
[user]
name = Reader
email = reader@example.com
Moving between two versions of a file
The clearest way to see what switching does is to read the same file on both sides. The repository below is a throwaway one in a temporary folder, and every example on this page starts from it.
mkdir ask-docs && cd ask-docs && git init -q -b main
export GIT_AUTHOR_DATE="2026-10-12T10:00:00+00:00" GIT_COMMITTER_DATE="2026-10-12T10:00:00+00:00"
printf 'You are a support assistant.\n' > prompt.txt
printf 'chunk_size = 500\n' > loader.py
git add . && git commit -q -m "Add the prompt and the loader"
cat prompt.txtYou are a support assistant.
That is the prompt as main holds it. The experiment is going to rewrite it.
Creating a branch and committing on it
git switch -c shorter-prompt
printf 'Answer in two sentences.\n' > prompt.txt
git commit -q -am "Ask for two sentence answers"
cat prompt.txtSwitched to a new branch 'shorter-prompt' Answer in two sentences.
- Switched to a new branch 'shorter-prompt' is
-cdoing two things: creating the name and moving you onto it. - The file holds the new prompt while you are on the experiment, and the commit you made went to the experiment rather than to
main.
Switching back
git switch main
cat prompt.txtSwitched to branch 'main' You are a support assistant.
Switched to branch 'main', and the file holds the old prompt again. Git rewrote it from the commit main points at. Nothing was lost: the other version is safe in the commit on the other branch.
git switch - goes back to where you came from, the way cd - does in a shell. It saves a lot of typing when you are moving between two branches.
Switching with work you have not committed
Git refuses to throw away changes you have not committed. If the file you edited differs between the two branches, the switch is refused and nothing moves.
printf 'You are a careful support assistant.\n' > prompt.txt # edited, not committed
git switch shorter-prompt
printf -- '--- the file is untouched ---\n'
cat prompt.txterror: Your local changes to the following files would be overwritten by checkout: prompt.txt Please commit your changes or stash them before you switch branches. Aborting --- the file is untouched --- You are a careful support assistant.
- error: Your local changes ... would be overwritten by checkout names the file in the way. Git stops before touching anything.
- Aborting means you are still on
mainand your edit is still there, which thecatconfirms. - The fix is to decide what the edit is worth. Commit it on the branch you are on, or throw it away with
git restore prompt.txt, or put it aside withgit stash, which the undoing part covers.
An edit to a file that is the same on both branches is carried across instead of refused, and git status on the other side still shows it as modified. That is convenient and surprising in equal measure, so run git status before you switch.
Landing on a commit instead of a branch
You can also move to a single commit. Git then has no branch to move forward, a state it calls detached HEAD. It is useful for looking at an old version and dangerous for working in.
printf 'chunk_size = 800\n' > loader.py
git commit -q -am "Raise the chunk size to 800"
git switch --detach @~1
git statusHEAD is now at 54f17c3 Add the prompt and the loader HEAD detached at 54f17c3 nothing to commit, working tree clean
- @~1 means one commit before where you are.
HEAD~1is the same thing written out, and a short identifier fromgit log --onelineworks too. - HEAD detached at ... says you are on a commit and not on a branch. The files are the ones from that commit, so this is how you look at an old version of the project.
- A commit made here belongs to no branch and is easy to lose. If you want to keep work started in this state, make a branch for it with
git switch -c <name>.
Getting back to a branch
git switch -Previous HEAD position was 54f17c3 Add the prompt and the loader Switched to branch 'main'
Previous HEAD position was ... names the commit you were standing on, and the line under it says you are on a branch again. git switch - takes you back to the branch you left, whichever one it was.
switch compared with checkout
git checkout does several unrelated jobs, which is why Git 2.23 split it into git switch for branches and git restore for files. Both old and new commands work, and you will meet both.
| Job | Current command | Older command |
|---|---|---|
| Move to a branch | git switch main | git checkout main |
| Create it and move | git switch -c fix | git checkout -b fix |
| Move to a commit | git switch --detach <id> | git checkout <id> |
| Throw away an edit | git restore <file> | git checkout -- <file> |
| Unstage a file | git restore --staged <file> | git reset <file> |
Where you switch
- Starting an experiment.
git switch -c chunk-800creates it and moves you in one line. - Comparing two versions by hand. Switch, run the thing, switch back, run it again.
- Reading an old version.
git switch --detachon an old commit, look, thengit switch -.
main. Commit or stash before you move, and read the first line of git status after you land.Related
- Previous: Branches
- Next: Merging a branch
- See also: Staging and committing for the
git restorecommands named above - Reference: git switch and git checkout in the Git reference
- Switch to a branch, create a file, commit it, switch back and run
ls. The file is not there, and it comes back when you switch again. - Run
git switch nopefor a branch that does not exist and read what Git suggests. - Edit a file, run
git switch -c scratchand thengit status. The edit came with you, because the new branch starts from the same commit.
Every expert started right here.