サイトアイコンmaita tomoya dev io

#4 Git上級者向けTips集:効率的なバージョン管理

Git上級者向けTips集:効率的なバージョン管理

日常の開発作業を効率化するGitの上級テクニックを紹介します。

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

コミット履歴を整理する強力な機能です。

# 直近3つのコミットを編集
git rebase -i HEAD~3

# コミットの順序変更、結合、編集が可能
pick abc1234 First commit
squash def5678 Second commit
reword ghi9012 Third commit

2. 部分的なステージング

# ファイルの一部のみをステージング
git add -p

# 各変更に対して以下のオプションを選択
# y - この変更をステージング
# n - この変更をスキップ
# s - 変更を分割
# e - 手動で編集

3. bisectによるバグの特定

# bisectの開始
git bisect start
git bisect bad  # 現在のコミットはバグあり
git bisect good abc1234  # このコミットは正常

# Gitが自動的に中間のコミットをチェックアウト
# テスト後、good/badを指定していく
git bisect good  # または git bisect bad

# バグを引き起こしたコミットが特定される

4. reflogで失われたコミットを復元

# 参照ログを確認
git reflog

# 失われたコミットを復元
git checkout HEAD@{2}
# または
git reset --hard HEAD@{2}

5. エイリアスの活用

.gitconfigに便利なエイリアスを設定:

[alias]
    st = status -s
    co = checkout
    br = branch
    cm = commit -m
    unstage = reset HEAD --
    last = log -1 HEAD
    visual = !gitk
    lg = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset'

6. フックの活用

.git/hooks/pre-commitでコミット前の自動チェック:

#!/bin/sh
# TypeScriptの型チェック
npm run type-check
if [ $? -ne 0 ]; then
    echo "Type check failed. Please fix errors before committing."
    exit 1
fi

# Lintチェック
npm run lint
if [ $? -ne 0 ]; then
    echo "Lint check failed. Please fix errors before committing."
    exit 1
fi

7. ワークツリーの活用

複数のブランチを同時に作業:

# 新しいワークツリーを作成
git worktree add ../project-feature feature-branch

# ワークツリー一覧
git worktree list

# 不要になったワークツリーを削除
git worktree remove ../project-feature

8. 賢いマージ戦略

# ours戦略:現在のブランチを優先
git merge -s ours other-branch

# theirs戦略:マージするブランチを優先(特定ファイルのみ)
git checkout --theirs path/to/file
git add path/to/file

# no-ffオプション:必ずマージコミットを作成
git merge --no-ff feature-branch

これらのテクニックをマスターすることで、より効率的で柔軟なバージョン管理が可能になります。