ストーリー
間違いのパターン
Gitで「間違えた」と感じる状況は、大きく4つあります。
| パターン | 状況 | 解決策 |
|---|---|---|
| ① | 編集したけど、やっぱり元に戻したい | git checkout / git restore |
| ② | add したけど、ステージングを取り消したい | git reset / git restore --staged |
| ③ | commit したけど、取り消したい | git reset / git revert |
| ④ | push したけど、取り消したい | git revert |
図で理解しよう
graph LR
Working["ワーキング<br/>ディレクトリ"]
Staging["ステージング<br/>エリア"]
Repo["リポジトリ<br/>(ローカル)"]
Remote["リモート<br/>(GitHub)"]
Working -->|"add"| Staging
Staging -->|"commit"| Repo
Repo -->|"push"| Remote
Staging -->|"① checkout"| Working
Repo -->|"② reset"| Staging
Repo -->|"③ reset"| Working
Remote <-->|"④ revert"| Repo
style Working fill:#dbeafe,stroke:#2563eb,stroke-width:2px,color:#1e40af
style Staging fill:#fef3c7,stroke:#d97706,stroke-width:2px,color:#92400e
style Repo fill:#d1fae5,stroke:#059669,stroke-width:2px,color:#064e3b
style Remote fill:#fce7f3,stroke:#db2777,stroke-width:2px,color:#831843
パターン①: ファイルの変更を取り消す
状況
ファイルを編集したけど、保存前の状態に戻したい。
hello.txt を編集 → やっぱり元に戻したい
解決策
# 従来の方法
git checkout -- hello.txt
# 新しい方法(Git 2.23以降)
git restore hello.txt
編集内容は完全に失われます!取り消せません。
パターン②: ステージングを取り消す
状況
git add したけど、ステージングを取り消したい。
git add hello.txt → やっぱりステージングを取り消したい
解決策
# 従来の方法
git reset HEAD hello.txt
# 新しい方法(Git 2.23以降)
git restore --staged hello.txt
ファイルの内容は変わりません。ステージングだけ取り消されます。
パターン③: コミットを取り消す
状況
git commit したけど、コミット自体を取り消したい。
git commit -m "..." → やっぱりコミットを取り消したい
解決策A: resetで履歴ごと削除
git reset --soft HEAD~1 # コミットだけ取り消し(変更は残る)
git reset --mixed HEAD~1 # コミット+ステージング取り消し(変更は残る)
git reset --hard HEAD~1 # すべて取り消し(変更も消える)
解決策B: revertで打ち消しコミット
git revert HEAD
新しいコミットで変更を打ち消します。履歴は残ります。
パターン④: pushしたコミットを取り消す
状況
git push してしまった。リモートの変更を取り消したい。
解決策
git revert HEAD
git push
重要: push済みのコミットには git reset
を使わない!チームに迷惑がかかります。
取り消しコマンドの選び方
graph TD
Q1{"まだ add していない?"}
Q2{"まだ commit していない?"}
Q3{"まだ push していない?"}
A1["git checkout / git restore"]
A2["git reset HEAD / git restore --staged"]
A3["git reset --soft/mixed/hard"]
A4["git revert"]
Q1 -->|"Yes"| A1
Q1 -->|"No"| Q2
Q2 -->|"Yes"| A2
Q2 -->|"No"| Q3
Q3 -->|"Yes"| A3
Q3 -->|"No"| A4
style Q1 fill:#dbeafe,stroke:#2563eb,stroke-width:2px,color:#1e40af
style Q2 fill:#fef3c7,stroke:#d97706,stroke-width:2px,color:#92400e
style Q3 fill:#fce7f3,stroke:#db2777,stroke-width:2px,color:#831843
style A1 fill:#d1fae5,stroke:#059669,color:#064e3b
style A2 fill:#d1fae5,stroke:#059669,color:#064e3b
style A3 fill:#d1fae5,stroke:#059669,color:#064e3b
style A4 fill:#fee2e2,stroke:#dc2626,color:#991b1b
新旧コマンドの対応
Git 2.23で git restore と git switch が追加されました。
| 操作 | 従来のコマンド | 新しいコマンド |
|---|---|---|
| 変更を取り消す | git checkout -- ファイル | git restore ファイル |
| ステージング取り消し | git reset HEAD ファイル | git restore --staged ファイル |
| ブランチ切り替え | git checkout ブランチ | git switch ブランチ |
新しいコマンドの方が意図が明確で分かりやすいです。
危険度レベル
| コマンド | 危険度 | 説明 |
|---|---|---|
git restore --staged | 🟢 低 | ステージングを取り消すだけ |
git restore | 🟡 中 | 変更が失われる |
git reset --soft | 🟢 低 | コミットだけ取り消し |
git reset --mixed | 🟡 中 | コミット+ステージング取り消し |
git reset --hard | 🔴 高 | すべての変更が失われる |
git push --force | 🔴 危険 | リモートの履歴を書き換え |
取り消す前に確認しよう
現在の状態を確認
git status
- Untracked files → まだaddしていない
- Changes not staged → addしていない変更
- Changes to be committed → add済み
- nothing to commit → すべてコミット済み
履歴を確認
git log --oneline -5
どのコミットを取り消すか確認。
よくある間違い
間違い1: checkout と reset を混同
git checkout HEAD~1 # ブランチが移動してしまう!
git reset HEAD~1 # コミットを取り消す
間違い2: —hard を安易に使う
git reset --hard HEAD~1 # 変更が完全に消える!
まずは --soft で試しましょう。
間違い3: push後にresetを使う
git reset --hard HEAD~1
git push --force # チームに迷惑!
push後は git revert を使いましょう。
ハンズオン
まずは状況を確認するコマンドを練習しましょう。
cd ~/my-first-git
# 1. 現在の状態を確認
git status
# 2. 履歴を確認
git log --oneline -3
# 3. 変更を作成
echo "テスト" >> hello.txt
# 4. 状態を確認(Changes not staged)
git status
# 5. ステージング
git add hello.txt
# 6. 状態を確認(Changes to be committed)
git status
まとめ
| パターン | 状況 | コマンド |
|---|---|---|
| ① | 編集を取り消したい | git restore ファイル |
| ② | ステージングを取り消したい | git restore --staged ファイル |
| ③ | コミットを取り消したい(ローカル) | git reset |
| ④ | コミットを取り消したい(push済み) | git revert |
判断のポイント
- add前 →
git restore - add後、commit前 →
git restore --staged - commit後、push前 →
git reset - push後 →
git revert
チェックリスト
- 4つの取り消しパターンを理解できた
- 各パターンで使うコマンドがわかった
- 危険度を意識できるようになった
次のステップへ
取り消しパターンの概要は理解できましたか?
次のセクションから、各コマンドを実際に使ってみましょう。まずは git checkout /
git restore から始めます!
推定読了時間: 30分