# git エイリアス — ショートカットを作ろう

<!-- prev/next navigation -->
[< Previous: コミットメッセージの書き方 — Conventional Commits](./09-commit-conventions.md) | [Back to Index](../../../README.md) |

## What & Why

`git status` を1日に何十回も打っていると、さすがに面倒になってくる。git にはエイリアス（alias）機能があって、長いコマンドに短い名前をつけられる。

エイリアスは `~/.gitconfig` に保存される——[セットアップセクション](../01-setup/01-gitconfig-user.md)で触れたあのファイルだ。設定は一度だけ。あとはずっと使い続けられる。

## Content

### エイリアスを設定する

`git config --global alias.<名前> <コマンド>` で設定する。例えば `git config --global alias.st status` を実行すると、`git st` と打つだけで `git status` が動くようになる。

---

### おすすめのエイリアスセット

よく使われる定番エイリアスをまとめて設定しておくと便利だ：

| エイリアス | 元のコマンド | 用途 |
|---|---|---|
| `st` | `status` | 状態確認。一番打つコマンド |
| `co` | `checkout` | ブランチ切り替え |
| `br` | `branch` | ブランチ一覧 |
| `unstage` | `reset HEAD --` | ステージから取り消す |
| `lg` | `log --oneline --graph --all --decorate` | ブランチグラフ付きログ |

`lg` は特に便利で、`--graph` オプション（[P1-025](../03-branching/08-exercise.md)で紹介）を毎回打たなくて済む。

---

### 設定を確認する

エイリアスは `~/.gitconfig` に保存される。`cat ~/.gitconfig` で確認してみよう（gitconfig の読み方は[P1-009](../01-setup/08-exercise-verify-gitconfig.md)参照）：

```bash
cat ~/.gitconfig
```

```
[user]
	name = Your Name
	email = your.email@example.com

[alias]
	st = status
	co = checkout
	br = branch
	unstage = reset HEAD --
	lg = log --oneline --graph --all --decorate
```

`[alias]` セクションに設定したエイリアスが並んでいる。

---

### シェルコマンドをエイリアスにする

`!` を先頭につけると、git コマンドではなくシェルコマンドとして実行できる。例えば `git config --global alias.root 'rev-parse --show-toplevel'` を設定すると、`git root` でリポジトリのルートディレクトリを表示できる。git 本体に関係ないコマンドでも組み合わせられる。

---

### エイリアスを削除したいとき

設定を消すには `git config --global --unset alias.<名前>` を使う。または `~/.gitconfig` をエディタで直接編集して該当行を削除しても OK。

## Summary

- `git config --global alias.<名前> <コマンド>` でエイリアスを設定できる。
- エイリアスは `~/.gitconfig` の `[alias]` セクションに保存される。
- `st`・`co`・`br`・`lg` は特によく使われる定番エイリアス。
- `lg`（`log --oneline --graph --all --decorate`）はブランチの全体像を一瞬で把握できる。
- `!` を先頭につけるとシェルコマンドもエイリアスにできる。

## Exercises

### ステップ 1: 定番エイリアスをまとめて設定する

<div class="code-input">

```bash
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.unstage 'reset HEAD --'
git config --global alias.lg 'log --oneline --graph --all --decorate'
```

</div>

### ステップ 2: 動作確認する

適当なリポジトリで試してみよう：

<div class="code-input">

```bash
git st
git br
git lg
```

</div>

それぞれ `git status`、`git branch`、`git log --oneline --graph --all --decorate` と同じ結果が表示されることを確認しよう。

### ステップ 3: gitconfig を確認する

<div class="code-input">

```bash
cat ~/.gitconfig
```

</div>

<div class="code-output">

```
[user]
	name = Your Name
	email = your.email@example.com

[alias]
	st = status
	co = checkout
	br = branch
	unstage = reset HEAD --
	lg = log --oneline --graph --all --decorate
```

</div>

`[alias]` セクションに設定が反映されていることを確認しよう。

### ステップ 4: unstage を試す

<div class="code-input">

```bash
mkdir alias-practice
cd alias-practice
git init
echo "test" > file.txt
git add file.txt
git st
```

</div>

<div class="code-output">

```
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	new file:   file.txt
```

</div>

<div class="code-input">

```bash
git unstage file.txt
git st
```

</div>

ステージから取り消せていることを確認しよう。

### Reset & Retry

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

<div class="code-input">

```bash
git config --global --unset alias.st
git config --global --unset alias.co
git config --global --unset alias.br
git config --global --unset alias.unstage
git config --global --unset alias.lg
```

</div>

その後ステップ1から再挑戦しよう。

---

## 🎉 Part 1 完走おめでとう！

ここまで来たあなたは、git の基本から応用まで一通りマスターした。

`git init` からはじまって、コミット、ブランチ、マージ、リベース、タグ、stash、reset、revert、reflog、cherry-pick、bisect、rebase -i、worktree、そしてエイリアスまで——プロの開発者が日常で使うツールセットが揃った。

**Part 2 では GitHub とリモートリポジトリを扱う。** ローカルで学んだことをチームで活かす方法——push、pull、プルリクエスト、フォーク、コードレビュー——を一緒に学んでいこう。

<!-- prev/next navigation -->
[< Previous: コミットメッセージの書き方 — Conventional Commits](./09-commit-conventions.md) | [Back to Index](../../../README.md) |
