src: https://www.r-staffing.co.jp/engineer/entry/20200131_1
## 概要
[[git checkout ワーキングディレクトリでの作業を元に戻す|git checkout]] から切り出された機能として [[git restoreとgit switchの新コマンド|restore と switch の新コマンド]] がある。`git switch` はブランチの作成や切り替えを行うコマンド。
## Synopsis
docs: https://git-scm.com/docs/git-switch
```sh:synopsis
git switch [<options>] [--no-guess] <branch>
git switch [<options>] --detach [<start-point>]
git switch [<options>] (-c|-C) <new-branch> [<start-point>]
git switch [<options>] --orphan <new-branch>
```
[[シノプシスとは|シノプシス]] も参照。
## 使い方
ブランチを切り替える時には同じ使い方。
```sh
# develop ブランチに切り替える
git checkout develop
# main ブランチに切り替える
git switch main
```
ブランチの新規作成&チェックアウトは `git switch -c/--create` オプションを実行する。
```sh
# feature/A ブランチを作成して切り替える
git checkout -b feature/A
# feature/B ブランチを作成して切り替える
git switch -c feature/B
git switch --create feature/C
```
以下のように `featureB` から `featureA` を派生させることもできる。
```sh
git switch -c new old
# featureBからfeatureAを派生
git switch -c featureA featureB
# mainブランチからfeatブランチを派生
git switch -c feat main
```
### 強制的にブランチを作成する
`-C, --force-create` オプションはすでにその名前のブランチがある場合にも強制的に作成する。
```sh
git switch -C feature/A
git switch --force-create feature/B
```
### 前のブランチに戻る
[[CLI cd|cd]] コマンドの `cd -` のように `-` を指定すことで、前にいたブランチに戻ることができる。
```sh
git switch -
```