git switch
git
Usage
git switch [-c <new>] <branch>The git 2.23 command that only moves between branches; it never rewrites file contents, which is what restore is for.
Common flags
| Flag | Meaning |
|---|---|
| -c <new> | create the branch and switch to it |
| -C <new> | create it, or reset it here if it exists |
| - | go back to the branch you were on before |
| --detach <commit> | check out a commit without a branch |
| --track <remote>/<branch> | follow a remote branch |
| -m | carry uncommitted changes across by merging them |
Examples
git switch mainmoves to main; refuses if that would clobber your edits
git switch -c feature/apicreates the branch and moves there
git switch -jumps back to the previous branch
The confusion is that undo has several shapes — moving where a branch points is not the same as adding a commit that reverses it.
How to read this
- Square brackets [ ] mark a part you may leave out.
- An ellipsis … means you can list more than one.
- Flags are case-sensitive — in some commands -r and -R do different things.
Questions
Q. What does git switch do?
The git 2.23 command that only moves between branches; it never rewrites file contents, which is what restore is for.
Q. How do I type it?
git switch [-c <new>] <branch> — square brackets mark the parts you can leave out.
Q. How many flags are worth knowing?
6 are listed here; the full set is in man git. This command sits under git.
Related commands
man page: man git-switch