困りごと:間違えた!元に戻したい!
😱 こんなミスありませんか?
- 「重要な部分を間違えて削除してしまった!」
- 「動いていたコードを壊してしまった...」
- 「間違ったファイルを編集してしまった」
- 「元のバージョンに戻したいけど、どうすれば...」
安心してください! Gitなら簡単に元に戻せます。
3つの状態と取り消し方法
Gitでは、ファイルの状態によって取り消し方法が異なります:
作業ディレクトリ → ステージングエリア → リポジトリ
(編集中) (コミット準備) (履歴保存済み)
↓ ↓ ↓
git restore git restore --staged git revert
1. 作業ディレクトリの変更を取り消す
ケース:ファイルを編集したが、元に戻したい
# プロジェクトを準備
mkdir restore-practice
cd restore-practice
git init
# ファイルを作成してコミット
echo "function calculate() {
return 42;
}" > app.js
git add app.js
git commit -m "初期実装"
# ファイルの内容を確認
cat app.js
次に、ファイルを間違えて編集してしまった場合:
# 間違えて重要な部分を削除
echo "// 削除してしまった!" > app.js
cat app.js
# 状態を確認
git status
# 元に戻す
git restore app.js
# 確認
cat app.js
元の内容が復元されました!
複数ファイルを一度に戻す
# 複数のファイルを変更
echo "変更1" > file1.txt
echo "変更2" > file2.txt
echo "変更3" > file3.txt
git add .
git commit -m "3つのファイルを追加"
# すべて編集
echo "間違った変更" >> file1.txt
echo "間違った変更" >> file2.txt
echo "間違った変更" >> file3.txt
# すべて元に戻す
git restore .
# 確認
cat file1.txt file2.txt file3.txt
2. ステージングエリアから取り消す
ケース:git addしたけど、やっぱりコミットしたくない
# 新しいファイルを作成
echo "まだ完成していない機能" > incomplete.js
# 間違えてステージングに追加
git add incomplete.js
git status
# ステージングから取り消す(ファイルは残る)
git restore --staged incomplete.js
git status
# ファイルは残っている
ls
cat incomplete.js
3. 特定のコミットの状態に戻す
ケース:3つ前のバージョンに戻したい
# 履歴を作成
echo "v1.0" > version.txt
git add version.txt
git commit -m "バージョン1.0"
echo "v1.1" > version.txt
git add version.txt
git commit -m "バージョン1.1"
echo "v1.2" > version.txt
git add version.txt
git commit -m "バージョン1.2"
echo "v1.3 バグあり!" > version.txt
git add version.txt
git commit -m "バージョン1.3"
# 履歴を確認
git log --oneline
# 現在の内容
cat version.txt
特定のコミットの状態に戻す:
# コミットIDを確認
git log --oneline
# 特定のバージョンに戻す(コミットIDの最初の数文字でOK)
# git restore --source=コミットID version.txt
# または、相対的に指定
git restore --source=HEAD~2 version.txt
# 確認
cat version.txt
git status
よくあるシナリオと対処法
シナリオ1:すべての変更を破棄したい
# たくさんの変更をしてしまった
echo "変更" >> app.js
echo "新規ファイル" > new.txt
echo "別の変更" > another.txt
git status
# すべての追跡ファイルの変更を破棄
git restore .
# 未追跡ファイルも削除したい場合
git clean -f
git status
シナリオ2:一部の変更だけ残したい
# ファイルに複数の変更
echo "function add(a, b) {
return a + b;
}
function subtract(a, b) {
return a - b;
}
// バグがある関数
function multiply(a, b) {
return a + b; // バグ:足し算になっている
}" > math.js
git add math.js
git commit -m "数学関数を追加"
# 対話的に一部だけ戻す
git restore -p math.js
シナリオ3:削除したファイルを復元
# ファイルを作成してコミット
echo "重要なファイル" > important.txt
git add important.txt
git commit -m "重要なファイルを追加"
# 間違えて削除
rm important.txt
ls
# 状態を確認
git status
# 復元
git restore important.txt
ls
cat important.txt
git restore以外の方法
git checkout(従来の方法)
# 従来の方法(まだ使えるが、restoreが推奨)
echo "変更" >> app.js
git checkout -- app.js
# または
git checkout HEAD -- app.js
git reset(より強力だが注意が必要)
# ステージングエリアから取り消す
git reset HEAD file.txt
# コミット自体を取り消す(危険!)
git reset --hard HEAD~1
安全に作業するためのTips
1. 変更前に確認
# 何が変更されているか確認してから取り消す
git status
git diff
# 特定のファイルの差分を確認
git diff app.js
2. 部分的に戻す
# 対話モードで選択的に戻す
git restore -p file.txt
# yで適用、nでスキップ、qで終了
3. 一時的に保存(stash)
# 変更を一時的に保存
echo "作業中" >> app.js
git stash
# 元の状態に
git status
# 後で戻す
git stash pop
まとめ
変更の取り消し方法を学びました!
- ✅ 作業中の変更は
git restore ファイル名
で取り消し - ✅ ステージングからは
git restore --staged
で取り消し - ✅ 特定のコミットの状態には
git restore --source=
で戻す - ✅ 削除したファイルも簡単に復元できる
重要な原則:
- Gitで管理されているファイルは、ほぼ必ず復元可能
- 慌てずに、まず
git status
で状況確認 - 破壊的な操作の前には、必ずバックアップかコミット
次のレッスンでは、GitHubを使ってクラウド上でコードを管理する方法を学びます!
【実践演習】変更の取り消し操作
実際にプロジェクトを作成して、様々な取り消し操作を練習しましょう。
CommandAcademy Terminal
Welcome to CommandAcademy Terminal!
Type "help" to see available commands.
user@cmdac:~$
█
ファイルツリー
/
etc
hosts35B
passwd76B
home
user
tmp
usr
bin
share
var
log
練習課題
-
作業ディレクトリの変更を取り消す
config.js
の変更をgit restore
で元に戻す- ファイルの内容を確認
-
ステージングエリアから取り消す
- 新しいファイル
test.js
を作成してgit add
git restore --staged
で取り消す- ファイルは残っていることを確認
- 新しいファイル
-
削除したファイルを復元
style.css
を削除(rm
コマンド)git restore
で復元
-
特定のコミットの状態に戻す
- 何回かコミットを作成
git log --oneline
で履歴を確認git restore --source=HEAD~1
で1つ前の状態に戻す
CommandAcademy Terminal
Welcome to CommandAcademy Terminal!
Type "help" to see available commands.
user@cmdac:~$
█
ファイルツリー
/
etc
hosts35B
passwd76B
home
user
tmp
usr
bin
share
var
log
🎯 確認ポイント
git restore
とgit restore --staged
の違いを理解できた- 削除したファイルも簡単に復元できることを確認した
- 特定のコミットの状態に戻す方法を習得した
- どんなミスも怖くない自信がついた!