git-advanced-workflows について
Git の高度な操作を扱うスキルガイドです。履歴の整理、ブランチでの協業、トラブルからの復旧を、コマンドと注意点とともに説明します。
主な機能
- 01インタラクティブ rebase による履歴整理
- 02ブランチ間でのコミット適用
- 03bisect による原因コミットの特定
- 04worktree での複数ブランチ作業
- 05reflog を使ったコミット復旧
活用シーン
- 01マージ前の履歴整理
- 02不具合を持ち込んだコミットの追跡
- 03複数機能ブランチの並行開発
- 04削除または消失したコミットの復元
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ wshobson/ agents --skill git-advanced-workflowsインストール後、Claude Code で/git-advanced-workflowsを入力して読み込まれたか確認します。
同梱ファイルreferences/details.md
詳細
Git の高度なワークフロー
高度な Git のテクニックを習得し、クリーンな履歴を保ち、効果的に共同作業し、どんな状況からも自信を持って復旧できるようにします。
このスキルを使う場面
- マージ前にコミット履歴を整理する
- 特定のコミットを別のブランチに適用する
- バグを引き起こしたコミットを見つける
- 複数の機能に同時に取り組む
- Git のミスや失われたコミットから復旧する
- 複雑なブランチのワークフローを管理する
- レビュー用にクリーンな PR を準備する
- 分岐したブランチを同期する
主な概念
1. インタラクティブリベース
インタラクティブリベースは、Git の履歴編集に使える万能ツールです。
一般的な操作:
pick:コミットをそのまま保持しますreword:コミットメッセージを変更しますedit:コミットの内容を修正しますsquash:直前のコミットと統合しますfixup:コミットメッセージを破棄する点を除き、squash と同じですdrop:コミットを完全に削除します
基本的な使い方:
# Rebase last 5 commits
git rebase -i HEAD~5
# Rebase all commits on current branch
git rebase -i $(git merge-base HEAD main)
# Rebase onto specific commit
git rebase -i abc123
2. チェリーピック
ブランチ全体をマージせずに、特定のコミットを別のブランチに適用します。
# Cherry-pick single commit
git cherry-pick abc123
# Cherry-pick range of commits (exclusive start)
git cherry-pick abc123..def456
# Cherry-pick without committing (stage changes only)
git cherry-pick -n abc123
# Cherry-pick and edit commit message
git cherry-pick -e abc123
3. Git bisect
コミット履歴を二分探索して、バグを引き起こしたコミットを見つけます。
# Start bisect
git bisect start
# Mark current commit as bad
git bisect bad
# Mark known good commit
git bisect good v1.0.0
# Git will checkout middle commit - test it
# Then mark as good or bad
git bisect good # or: git bisect bad
# Continue until bug found
# When done
git bisect reset
bisect の自動化:
# Use script to test automatically
git bisect start HEAD v1.0.0
git bisect run ./test.sh
# test.sh should exit 0 for good, 1-127 (except 125) for bad
4. ワークツリー
スタッシュやブランチの切り替えをせずに、複数のブランチで同時に作業します。
# List existing worktrees
git worktree list
# Add new worktree for feature branch
git worktree add ../project-feature feature/new-feature
# Add worktree and create new branch
git worktree add -b bugfix/urgent ../project-hotfix main
# Remove worktree
git worktree remove ../project-feature
# Prune stale worktrees
git worktree prune
5. Reflog
安全網として、削除されたコミットを含むすべての参照の移動を追跡します。
# View reflog
git reflog
# View reflog for specific branch
git reflog show feature/branch
# Restore deleted commit
git reflog
# Find commit hash
git checkout abc123
git branch recovered-branch
# Restore deleted branch
git reflog
git branch deleted-branch abc123
詳細なパターンと実例
詳細なパターンのドキュメントは references/details.md にあります。上記のナビゲーションでは不十分な場合は、そのファイルをお読みください。
ベストプラクティス
- 必ず --force-with-lease を使う:--force より安全で、他の人の作業を上書きするのを防ぎます
- ローカルのコミットのみをリベースする:プッシュして共有済みのコミットはリベースしないでください
- わかりやすいコミットメッセージ:未来の自分が、今の自分に感謝するでしょう
- アトミックなコミット:各コミットは単一の論理的な変更にしてください
- 強制プッシュの前にテストする:履歴の書き換えで問題が起きていないことを確認してください
- Reflog を把握しておく:Reflog は安全網ですが、現在の先端から到達できないコミットのエントリはデフォルトで 30 日後に(
gc.reflogExpireUnreachable)、到達可能なエントリは 90 日後に(gc.reflogExpire)期限切れになります - リスクの高い操作の前にブランチを作成する:複雑なリベースの前にバックアップブランチを作成してください
# Safe force push
git push --force-with-lease origin feature/branch
# Create backup before risky operation
git branch backup-branch
git rebase -i main
# If something goes wrong
git reset --hard backup-branch
よくある落とし穴
- 公開ブランチをリベースする:共同作業者との履歴の競合を引き起こします
- リースなしで強制プッシュする:チームメイトの作業を上書きする可能性があります
- リベースで作業を失う:競合を慎重に解決し、リベース後にテストしてください
- ワークツリーのクリーンアップを忘れる:孤立したワークツリーがディスク容量を消費します
- 試行前のバックアップを怠る:必ず安全用のブランチを作成してください
- 作業ディレクトリが汚れた状態で bisect する:bisect の前にコミットするか、スタッシュしてください
復旧コマンド
# Abort operations in progress
git rebase --abort
git merge --abort
git cherry-pick --abort
git bisect reset
# Restore file to version from specific commit
git restore --source=abc123 path/to/file
# Undo last commit but keep changes
git reset --soft HEAD^
# Undo last commit and discard changes
# WARNING: --hard also deletes uncommitted changes (staged and unstaged),
# and the reflog cannot recover them. Check `git status` and stash first.
git reset --hard HEAD^
# Recover a deleted branch soon. By default, git gc can prune HEAD's reflog
# entries for unmerged commits once they're 30 days old, counted from when
# each entry was recorded, not from the deletion
# Deleting a branch also deletes its own reflog; search HEAD's reflog
git reflog
git branch recovered-branch abc123
コメント
Markdown 対応