# Git Switch: change branches

Published: 2026-11-30

Rights: original
Source: https://kairos-2z6s.onrender.com/res/git-switch-full-en
`git switch -c NAME` creates a branch and switches to it. `git switch NAME` switches to an existing branch, and `git switch -` returns to the previous one.

Each command below is the exact command that was run and each output block is copied from the recorded log. Test ids, log hashes and exit statuses are in the evidence table.

(Test: 3 tests were run, TE-71-A, TE-71-B and TE-71-C. The expected non-zero exit is TE-71-C (exit status 128). Every row has a recorded log, and the exit statuses and log hashes are in the evidence table.)

### TE-71-A: switch -c creates a branch, and switch followed by a single hyphen returns to the previous branch

Setup for this run, inside a throwaway directory (`mktemp -d`):

```bash
git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'a\n' > f.txt; git add f.txt; git commit -qm one
```

Command:

```bash
git switch -q -c feature; echo now=$(git branch --show-current); git switch -q -; echo now=$(git branch --show-current)
```

Recorded output:

```
now=feature
now=main
```

Exit status: 0. The output matches the expected text written before the run.

### TE-71-B: a file committed on the new branch is not in the working tree after switching back

Setup for this run, inside a throwaway directory (`mktemp -d`):

```bash
git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'one\n' > f.txt; git add f.txt; git commit -qm one
```

Command:

```bash
git switch -q -c feature; printf 'x\n' > g.txt; git add g.txt; git commit -qm two; git switch -q main; ls
```

Recorded output:

```
f.txt
```

Exit status: 0. This status and the output match what was written before the run.

### TE-71-C: expected non-zero exit: git switch to a branch name that does not exist stops with a fatal error

Setup for this run, inside a throwaway directory (`mktemp -d`):

```bash
git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'one\n' > f.txt; git add f.txt; git commit -qm one
```

Command:

```bash
git switch nosuch
```

Recorded output: none, standard output was empty.

Standard error from the same run:

```
fatal: invalid reference: nosuch
```

Exit status: 128. This status and the output match what was written before the run.

TE-71-B: g.txt was committed on the branch feature. After git switch -q main, ls lists only f.txt. TE-71-C: git switch nosuch exited with status 128 and standard error reads fatal: invalid reference: nosuch.

## Limits

The run has no uncommitted changes, so switching with a dirty working tree was not tested. Not tested on this page: `-C` (create or reset a branch and switch to it), `--guess`, `--discard-changes` (discard modifications in the working tree), `-m` (a 3-way merge with the new branch), `--conflict` (choose the merge or diff3 conflict style), `-d` (detach HEAD at the named commit), `-t` (set upstream information for the new branch), `-f`, `--orphan` (a new unparented branch), `--ignore-other-worktrees` and `--recurse-submodules`. The names and one-line descriptions come from `git switch -h` in git 2.34.1. No run here used them.

## Source

- https://git-scm.com/docs/git-switch

## Evidence table

| Test | What it checks | Exit | Verdict | Log sha256 (first 12) |
|---|---|---|---|---|
| `TE-71-A` | switch -c creates a branch, and switch followed by a single hyphen returns to the previous branch | 0 | PASS | `30a323189420` |
| `TE-71-B` | a file committed on the new branch is not in the working tree after switching back | 0 | PASS | `bffc4ecc2496` |
| `TE-71-C` | expected non-zero exit: git switch to a branch name that does not exist stops with a fatal error | 128 | PASS | `141fe52b59b9` |

Recorded on: bash 5.1.16(1)-release; git version 2.34.1; Python 3.10.12. Locale C.UTF-8, TZ=UTC.
Every row has a recorded log holding the setup commands, the command, stdout, stderr and exit status. The first 12 characters of the log's sha256 are in the table.

## Related guides
- [Git Shortlog: commits per author](https://kairos-2z6s.onrender.com/res/git-shortlog-full-en)
- [Git Restore: unstage or discard a file](https://kairos-2z6s.onrender.com/res/git-restore-full-en)
- [git tag: name a commit, lightweight and annotated](https://kairos-2z6s.onrender.com/res/git-tag-full-en)
- [Git Stash Include Untracked: set aside new files too](https://kairos-2z6s.onrender.com/res/git-stash-include-untracked-full-en)
- [Everyday Git: branches, stash, undo and merge conflicts](https://kairos-2z6s.onrender.com/res/git-workflows-hub-en)
- [Restore a deleted branch with git reflog](https://kairos-2z6s.onrender.com/res/restore-deleted-branch-full-en)
