mcpField
G
git-advanced

git-advanced

github.com/rohitg00開発2.8k185 回インストール24 週間前に更新
リポジトリ

git-advanced について

高度なリポジトリ操作を行う開発者向けのGitスキルです。worktree、bisect、インタラクティブrebase、フック、復旧方法、安全対策を取り上げます。

主な機能

  1. 01worktreeによる並行ブランチ作業
  2. 02bisectによるコミット調査
  3. 03インタラクティブrebase操作
  4. 04lintやチェック用のGitフック
  5. 05resetとreflogによる復旧

活用シーン

  1. 01スタッシュせずに複数ブランチで作業
  2. 02不具合を導入したコミットの特定
  3. 03マージ前のコミット整理
  4. 04削除したブランチやファイルの復旧

インストール

コマンドをコピーしてターミナルで実行します。

$npx skills add https://github.com/rohitg00/awesome-claude-code-toolkit --skill git-advanced

インストール後、Claude Code で/git-advancedを入力して読み込まれたか確認します。

詳細

Git の高度な使い方

ワークツリー

# Create a worktree for a feature branch (avoids stashing)
git worktree add ../feature-auth feature/auth

# Create a worktree with a new branch
git worktree add ../hotfix-123 -b hotfix/123 origin/main

# List all worktrees
git worktree list

# Remove a worktree after merging
git worktree remove ../feature-auth

ワークツリーを使うと、作業中の変更を退避したりコミットしたりせずに、複数のブランチで同時に作業できます。各ワークツリーには専用の作業ディレクトリがあり、同じ .git リポジトリを共有します。

二分探索

# Start bisect, mark current as bad and known good commit
git bisect start
git bisect bad HEAD
git bisect good v1.5.0

# Git checks out a midpoint commit. Test it, then mark:
git bisect good   # if this commit works
git bisect bad    # if this commit is broken

# Automate with a test script
git bisect start HEAD v1.5.0
git bisect run npm test

# When done, reset
git bisect reset

二分探索では、コミット間を二分探索して、バグが混入したコミットを特定します。run を使った自動二分探索が最も速い方法です。

インタラクティブリベース

# Rebase last 5 commits interactively
git rebase -i HEAD~5

# Common operations in the editor:
# pick   - keep commit as-is
# reword - change commit message
# edit   - stop to amend the commit
# squash - merge into previous commit, keep both messages
# fixup  - merge into previous commit, discard this message
# drop   - remove the commit entirely

# Rebase feature branch onto latest main
git fetch origin
git rebase origin/main

# Continue after resolving conflicts
git rebase --continue

# Abort if things go wrong
git rebase --abort

Git フック

#!/bin/sh
# .git/hooks/pre-commit

# Run linter on staged files only
STAGED_FILES=$(git diff --cached --name-only --diff-filter=d | grep -E '\.(ts|tsx|js|jsx)$')
if [ -n "$STAGED_FILES" ]; then
  npx eslint $STAGED_FILES --fix
  git add $STAGED_FILES
fi
#!/bin/sh
# .git/hooks/commit-msg

# Enforce conventional commit format
COMMIT_MSG=$(cat "$1")
PATTERN="^(feat|fix|docs|style|refactor|test|chore)(\(.+\))?: .{1,72}$"

if ! echo "$COMMIT_MSG" | head -1 | grep -qE "$PATTERN"; then
  echo "Error: Commit message must follow Conventional Commits format"
  echo "Example: feat(auth): add OAuth2 login flow"
  exit 1
fi
#!/bin/sh
# .git/hooks/pre-push

# Run tests before pushing
npm test
if [ $? -ne 0 ]; then
  echo "Tests failed. Push aborted."
  exit 1
fi

復旧方法

# Undo last commit but keep changes staged
git reset --soft HEAD~1

# Recover a deleted branch using reflog
git reflog
git checkout -b recovered-branch HEAD@{3}

# Recover a file from a specific commit
git checkout abc1234 -- path/to/file.ts

# Find lost commits (dangling after reset or rebase)
git fsck --lost-found
git show <dangling-commit-sha>

# Undo a rebase
git reflog
git reset --hard HEAD@{5}  # point before rebase started

便利なエイリアス

# ~/.gitconfig
[alias]
  lg = log --graph --oneline --decorate --all
  st = status -sb
  co = checkout
  unstage = reset HEAD --
  last = log -1 HEAD --stat
  branches = branch -a --sort=-committerdate
  stash-all = stash push --include-untracked
  conflicts = diff --name-only --diff-filter=U

避けるべきパターン

  • --force-with-lease を使わずに、共有ブランチへ強制プッシュする
  • すでにプッシュして共有されているコミットをリベースする
  • Git LFS を使わずに、大きなバイナリファイルをコミットする
  • git diff --staged を確認せずに git add . を使う
  • ビルド生成物、依存関係、シークレットに .gitignore を使わない
  • 頻繁にマージせず、長期間存続するフィーチャーブランチを維持する

チェックリスト

  • 変更の退避ではなく、ブランチ間の並行作業にワークツリーを使用している
  • git bisect run とテストコマンドを使って、バグの特定を自動化している
  • main にマージする前に、インタラクティブリベースでコミットを整理している
  • プリコミットフックでステージ済みファイルの lint を実行している
  • commit-msg フックでコミットメッセージの形式を適用している
  • 強制プッシュ時に --force ではなく --force-with-lease を使用している
  • 破壊的な操作の前に reflog を確認している
  • .gitignore にビルド出力、依存関係、環境ファイルを含めている

コメント