Search notes:

git switch

git switch changes to an existing branch or creates a new one.
Optionally a new branch could be created with either -c, -C, automatically from a remote branch of same name (see --guess), or detach the working tree from any branch with --detach, along with switching.
git switch is experimental, the behavior is subject to change.
Switch to a given branch:
git switch [<options>] [--no-guess] <branch>
Create a new branch. By default, the branch is created from the point in history where HEAD currently points to. The optional value for start point specifies a different point in history:
git switch [<options>] (-c|-C) <new-branch> [<start-point>]
git switch [<options>] --detach [<start-point>]
git switch [<options>] --orphan <new-branch>
git switch is a command that was introduced in Git 2.3 with the intent to reduce confusion with the git checkout command.
Make git branch named V1.1 the current branch:
git switch V1.1
Create a branch, named Rel-2-fix, from the commit identified by HEAD~42.
git switch -c Rel-2-fix HEAD~42
Check out a specific commit without creating a branch:
git switch --detach HEAD~42

Options

-c --create <new-branch> Create a new branch named <new-branch> starting at <start-point> before switching to the branch. This is a convenient shortcut for: git branch <new-branch> followed by git switch <new-branch>.
-C --force-create <new-branch> Similar to --create except that if <new-branch> already exists, it will be reset to <start-point>. This is a convenient shortcut for: git branch -f <new-branch> followed by git switch <new-branch>
-d --detach Switch to a commit for inspection and discardable experiments. See the DETACHED HEAD section in git checkout for details.
--guess / --no-guess If <branch> is not found but there does exist a tracking branch in exactly one remote (call it <remote>) with a matching name, treat as equivalent to git switch -c <branch> --track <remote>/<branch>. If the branch exists in multiple remotes and one of them is named by the checkout.defaultRemote configuration variable, we’ll use that one for the purposes of disambiguation, even if the <branch> isn’t unique across all remotes. Set it to e.g. checkout.defaultRemote=origin to always checkout remote branches from there if <branch> is ambiguous but exists on the origin remote. See also checkout.defaultRemote in git config. --guess is the default behavior. Use --no-guess to disable it. The default behavior can be set via the checkout.guess configuration variable.
-f --force Alias for --discard-changes
--discard-changes Proceed even if the index or the working tree differs from HEAD. Both the index and working tree are restored to match the switching target. If --recurse-submodules is specified, submodule content is also restored to match the switching target. This is used to throw away local changes.
-m --merge If you have local modifications to one or more files that are different between the current branch and the branch to which you are switching, the command refuses to switch branches in order to preserve your modifications in context. However, with this option, a three-way merge between the current branch, your working tree contents, and the new branch is done, and you will be on the new branch. When a merge conflict happens, the index entries for conflicting paths are left unmerged, and you need to resolve the conflicts and mark the resolved paths with git add (or git rm if the merge should result in deletion of the path).
--conflict= <style> The same as --merge option above, but changes the way the conflicting hunks are presented, overriding the merge.conflictStyle configuration variable. Possible values are "merge" (default) and "diff3" (in addition to what is shown by "merge" style, shows the original contents).
-q --quiet Suppress feedback messages.
--progress / --no-progress Progress status is reported on the standard error stream by default when it is attached to a terminal, unless --quiet is specified. This flag enables progress reporting even if not attached to a terminal, regardless of --quiet.
-t --track When creating a new branch, set up «upstream» configuration. -c is implied. See --track in git branch for details. If no -c option is given, the name of the new branch will be derived from the remote-tracking branch, by looking at the local part of the refspec configured for the corresponding remote, and then stripping the initial part up to the "*". This would tell us to use hack as the local branch when branching off of origin/hack (or remotes/origin/hack, or even refs/remotes/origin/hack). If the given name has no slash, or the above guessing results in an empty name, the guessing is aborted. You can explicitly give a name with -c in such a case.
--no-track Do not set up «upstream» configuration, even if the branch.autoSetupMerge configuration variable is true.
--orphan <new-branch> Create a new orphan branch, named <new-branch>. All tracked files are removed.
--ignore-other-worktrees git switch refuses when the wanted ref is already checked out by another worktree. This option makes it check the ref out anyway. In other words, the ref can be held by more than one worktree.
--recurse-submodules / --no-recurse-submodules Using --recurse-submodules will update the content of all active submodules according to the commit recorded in the superproject. If nothing (or --no-recurse-submodules) is used, submodules working trees will not be updated. Just like git submodule, this will detach HEAD of the submodules.

Relation to git checkout

git switch command corresponding git checkout command
git switch Rel-2 git checkout Rel-2
git switch -c Rel-3 git checkout -b Rel-3

See also

git switch to remote branch after cloning a repository.
git commands

Index