銀河鉄道

【Git】タグ+パッチ+WIPコミットで安全に改修を進めるコマンド

サムネイル
[Git]Tag + Patch + WIP Commit

Tag + Patch + WIP Commit

Tag

Take a full snapshot of your current state.

It’s like planting a flag so you can always come back to this exact point.

いまの状態を丸ごとスナップショット化

「この地点に戻れる」というフラグを立てる

Patch

Export all code differences to an external text file.

Even if the repository breaks, your changes can still be restored.

差分をテキスト化して外部ファイルに保存

リポジトリが壊れても変更だけは復旧できる。

WIP Commit

A temporary “save point” during work.

Lets you roll back in small steps and reorganize your history later.

作業中のセーブポイント。

小刻みに戻れるし、履歴を後でまとめ直すのも簡単。

By combining these three,

you can safely perform major local refactors without creating a new branch.

この3つを重ねることで、ブランチを切らなくてもローカルだけで安全に大改修できる。

Step 1: Immediate Backup (Tag + Patch)|タグとパッチで即保険

Check uncommitted changes and the latest commits

未コミット変更と、タグを打つ対象のコミットを確認

git status 
git log --oneline -n 3

Create a tag (bookmark point)

タグを打つ(しおりを残す)

git tag -a pre-refactor-2025-10-05 -m "before refactor"

This creates an annotated tag that saves the full snapshot.

注釈付きタグでスナップショットを保存

Prompt:

“Generate a Git command to create an annotated tag marking the current state as a pre-refactor snapshot. Explain what the command does.”

Save patch (diff file)

パッチ保存(差分を持ち出す)

git diff > WIP_pre_refactor.patch

This exports all uncommitted changes as a patch file.
If you have binary files, use the next step’s WIP commit instead.

未コミット差分をファイル化。

バイナリがある場合は後述のWIPコミットでカバー。

Prompt:

“Show the Git command to export all uncommitted changes into a patch file. Describe how to reapply or revert this patch later.”

Step 2: Local Save (WIP Commit)|WIPコミットで近場のセーブ

Stage all changes

git add -A

Create WIP commit

git commit -m "WIP: checkpoint before refactor"

Now you have:

  • Tag = distant recovery point
  • WIP commit = nearby recovery point
  • Patch = external safety net

これで「遠い復旧点=タグ」「近い復旧点=WIP」「外部復旧手段=パッチ」が揃う。

Prompt:

“Write the Git commands to stage all files and create a WIP commit labeled ‘checkpoint before refactor’. Explain why this helps in large refactors.”

Recovery Methods|失敗したときの戻し方

Return to last WIP commit

直近のWIPコミットに戻す

git reset --hard <commit_hash>

Return to tag snapshot

タグの時点に戻す(まるごと復旧)

git reset --hard pre-refactor-2025-10-05
Prompt:

“Give me the Git command to restore my repository exactly to the snapshot saved in tag ‘pre-refactor-2025-10-05’.”

Undo patch changes

パッチ逆適用で差分取り消し

git apply -R WIP_pre_refactor.patch
Prompt:

“Explain how to undo changes using a saved patch file in Git, including the git apply -R syntax.”

Use stash (for temp save without history)

stashを使う場合(履歴を汚さず退避)

git stash push -u -m "checkpoint" 
git stash apply stash@{0}
Prompt:

“Provide Git commands to stash all changes (including untracked files) safely and then reapply them later.”

Safe Work Cycle

  1. Create empty structures (new files/classes)
    • 器だけ作る(新ファイルやクラスを置く)
  2. Delegate one method at a time → test → small commit
    • 既存コードから1メソッドだけ委譲 → 動作確認 → 小コミット
  3. Repeat
    • 反復
  4. Clean up with a soft reset if you want a tidy history
    • 最後に git reset --soft でまとめて再コミット(履歴をきれいにしたい場合)

git reset --soft HEAD~3  # example
Prompt:

“After several WIP commits, how can I squash or soft-reset them into one clean commit before pushing?”

Checklist

☑️ Tag created?|タグ打った?
☑️ Patch saved?|パッチ保存した?
☑️ WIP commit done?|WIPコミットした?
☑️ Incremental test/commit cycle?|小刻みな委譲・テスト・小コミットを繰り返してる?
☑️ Recovery commands handy?|戻しコマンドを手元に控えてる?

Metaphor Summary

ConceptMeaning
TagA flag — your “return here anytime” marker
PatchA lifeboat — floats even if the repo sinks
WIPWater supply — keeps you alive during the journey

Tag is your flag,
patch is your lifeboat,
WIP is your water.

著者

author
月うさぎ

編集後記:
この記事の内容がベストではないかもしれません。