ブランチとマージ
並行開発でメインコードを守りながら新機能を開発する。
用語集
ブランチ
開発の分岐。並行作業の単位。
マージ
2つのブランチを統合する。
コンフリクト
同じ箇所の変更が競合。
Fast-forward
直線的なマージ(分岐なし)。
ブランチの作成と切り替え
# Create a new branch for feature developmentgit branch feature/user-authentication
# Switch to the new branchgit switch feature/user-authentication
# Create and switch in one command (recommended)git switch -c feature/shopping-cart実行結果
Switched to a new branch 'feature/shopping-cart'
Tip: ブランチ名は feature/, fix/, docs/ などのプレフィックスをつけると分かりやすい。
マージ(統合)
開発が完了したら、mainブランチにマージします。
# First, switch back to maingit switch main
# Merge the feature branchgit merge feature/user-authentication
# Delete the branch after successful mergegit branch -d feature/user-authenticationBad
# Bad: Working directly on maingit switch main# ... edit files ...git commit -m "Add login"# Breaks production if buggy!Good
# Good: Work on feature branchgit switch -c feature/login# ... edit files ...git commit -m "feat: Add login"git switch maingit merge feature/login # Safe mergeコンフリクト(競合)の解消
同じファイルの同じ箇所を複数人が編集すると、競合が発生します。
<<<<<<< HEAD// Current branch (main) versionfunction login(user) { return authenticate(user);}=======// Incoming branch (feature/login) versionfunction login(user, password) { return validate(user, password);}>>>>>>> feature/login 解消手順:
- <<<, ===, >>> マーカーを確認
- 正しい内容に手動で編集
git add <file>git commit
解消後
// Manually resolved: Combined both approachesfunction login(user, password) { if (!validate(user, password)) { return false; } return authenticate(user);}合格ライン
git switch -c でブランチを作成できる
git merge でマージできる
コンフリクトマーカーを読んで解消できる
ブランチ命名規則を説明できる
演習課題
課題1: ブランチ作成とマージ
feature/practice ブランチを作成し、ファイルを変更してコミット後、main にマージしてください。
課題2: コンフリクト解消
意図的にコンフリクトを起こし、コンフリクトマーカーを編集して解消してください。