Git Switch: change branches
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.
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):
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 oneCommand:
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=mainExit 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):
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 oneCommand:
git switch -q -c feature; printf 'x\n' > g.txt; git add g.txt; git commit -qm two; git switch -q main; lsRecorded output:
f.txtExit 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):
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 oneCommand:
git switch nosuchRecorded output: none, standard output was empty.
Standard error from the same run:
fatal: invalid reference: nosuchExit 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
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.
All full guides: All guides