# git checkout — git switch との関係

<!-- prev/next navigation -->
[< Previous: ブランチを作って切り替える](02-branch-switch.md) | [Back to Index](../../../README.md) | [Next: ブランチをマージする >](04-merge.md)

## What & Why

前のページで `git switch` を使ってブランチを切り替えました。このページでは「`git checkout`」という古いコマンドを紹介します。ネットの記事や古いドキュメントによく出てくるので、意味がわかるようになっておきましょう。

## Content

### git checkout って何？

`git switch` が登場する前、ブランチの切り替えはずっと `git checkout` で行われていました。2つは「ブランチを切り替える」という点では同じ動作をします。

どちらを使っても `hobby-section` ブランチに切り替わります。

- `git switch hobby-section`
- `git checkout hobby-section`

---

### ブランチの作成と切り替えも同じ

`git switch -c` に相当する操作も `git checkout` でできます。

| git switch | git checkout | やること |
|---|---|---|
| `git switch <branch>` | `git checkout <branch>` | ブランチを切り替える |
| `git switch -c <branch>` | `git checkout -b <branch>` | 作成して切り替える |

`-b` が「branch（ブランチ）の作成」に相当します。

---

### なぜ git switch が生まれたの？

`git checkout` はブランチの切り替えだけでなく、**ファイルの変更を元に戻す**ことにも使えました。

```bash
# ブランチの切り替え
git checkout main

# ファイルの変更を元に戻す（まったく別の操作！）
git checkout -- diary.md
```

同じコマンドなのに動作が全然違う。これが初心者にとって非常にわかりにくかったため、git 2.23（2019年リリース）で役割を分けた新しいコマンドが導入されました。

- **`git switch`** → ブランチの切り替え専用
- **`git restore`** → ファイルの変更を元に戻す専用

`git restore` については[後のページ](../04-advanced/03-restore.md)で詳しく扱います。

---

### どちらを使えばいい？

**新しく学ぶなら `git switch` を使いましょう。**

ただし `git checkout` は今でも現役です。以下の場面で目にします。

- 古い技術ブログや Stack Overflow の回答
- 少し前に書かれた公式ドキュメントや書籍
- 他の人が書いたスクリプトや手順書

「`git checkout main` って書いてあるけど何これ？」とならないよう、`git switch main` と同じ意味だと覚えておくだけで十分です。

---

### 覚えること

```bash
git checkout <branch>    # = git switch <branch>
git checkout -b <branch> # = git switch -c <branch>
```

これだけです。ファイルを元に戻す使い方（`git checkout -- <file>`）は今は覚えなくて大丈夫です。

## Summary

- `git checkout <branch>` は `git switch <branch>` と同じ動作。
- `git checkout -b <branch>` は `git switch -c <branch>` と同じ動作。
- `git checkout` はブランチ以外にもファイル操作もできたため、わかりにくかった。
- git 2.23 以降は `git switch`（ブランチ）と `git restore`（ファイル）に分離された。
- 新しく書くなら `git switch` 推奨。古い資料で `git checkout` を見ても慌てない。

## Exercises

`my-diary` リポジトリで以下を試して、`git checkout` が `git switch` と同じ動作をすることを確認してください。

### 演習 1: 現在のブランチを確認する

<div class="code-input">

```bash
git branch
```

</div>

<div class="code-output">

```
* main
```

</div>

---

### 演習 2: `git checkout` でブランチを切り替える

`experiment` ブランチがあれば切り替えてみよう。

<div class="code-input">

```bash
git checkout experiment
```

</div>

<div class="code-output">

```
Switched to branch 'experiment'
```

</div>

`git status` で切り替わったことを確認する。

<div class="code-input">

```bash
git status
```

</div>

<div class="code-output">

```
On branch experiment
nothing to commit, working tree clean
```

</div>

---

### 演習 3: `main` に戻る

<div class="code-input">

```bash
git checkout main
```

</div>

<div class="code-output">

```
Switched to branch 'main'
```

</div>

---

### 演習 4: `git checkout -b` で新しいブランチを作って切り替える

<div class="code-input">

```bash
git checkout -b checkout-test
```

</div>

<div class="code-output">

```
Switched to a new branch 'checkout-test'
```

</div>

`git branch` で `*` の位置を確認する。

<div class="code-input">

```bash
git branch
```

</div>

<div class="code-output">

```
* checkout-test
  main
```

</div>

---

### 演習 5: `git switch` で `main` に戻る

`git switch` でも `git checkout` でもどちらでも OK。

<div class="code-input">

```bash
git switch main
```

</div>

<div class="code-output">

```
Switched to branch 'main'
```

</div>

---

### Reset & Retry

⚠️ うまくいかなかったときだけ実行してください。

<div class="code-input">

```bash
git switch main
git branch -d checkout-test
```

</div>

<div class="code-input">

```bash
git branch
```

</div>

<div class="code-output">

```
* main
```

</div>

<!-- prev/next navigation -->
[< Previous: ブランチを作って切り替える](02-branch-switch.md) | [Back to Index](../../../README.md) | [Next: ブランチをマージする >](04-merge.md)
