LESSON 30分

ストーリー

あなた
あ、間違えた…どうしよう
先輩
Gitなら大丈夫。取り消す方法はいくつかあるよ
あなた
いくつか?どれを使えばいいんですか?
先輩
状況によって使い分けるんだ。まずはパターンを理解しよう

間違いのパターン

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 restoregit 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

判断のポイント

  1. add前git restore
  2. add後、commit前git restore --staged
  3. commit後、push前git reset
  4. push後git revert

チェックリスト

  • 4つの取り消しパターンを理解できた
  • 各パターンで使うコマンドがわかった
  • 危険度を意識できるようになった

次のステップへ

取り消しパターンの概要は理解できましたか?

次のセクションから、各コマンドを実際に使ってみましょう。まずは git checkout / git restore から始めます!


推定読了時間: 30分