Git workflow patterns including branching strategies, commit conventions, merge vs rebase, conflict resolution, and collaborative development best practices for teams of all sizes.
Git workflow patterns including branching strategies, commit conventions, merge vs rebase, conflict resolution, and collaborative development best practices for teams of all sizes.
origin
ECC
Git Workflow Patterns
Git バージョン管理、ブランチ戦略、協調的開発のベストプラクティスです。
起動条件
新しいプロジェクトの Git ワークフローをセットアップする場合
ブランチ戦略を決定する場合(GitFlow、trunk-based、GitHub flow)
コミットメッセージや PR の説明を書く場合
マージコンフリクトを解決する場合
リリースとバージョンタグを管理する場合
新しいチームメンバーに Git プラクティスをオンボーディングする場合
ブランチ戦略
GitHub Flow(シンプル、ほとんどの場合推奨)
継続的デプロイメントおよび小〜中規模チームに最適です。
main (protected, always deployable)
│
├── feature/user-auth → PR → merge to main
├── feature/payment-flow → PR → merge to main
└── fix/login-bug → PR → merge to main
ルール:
main は常にデプロイ可能
main からフィーチャーブランチを作成
レビュー準備ができたら Pull Request を作成
承認と CI パス後に main にマージ
マージ後すぐにデプロイ
Trunk-Based Development(高速チーム向け)
強力な CI/CD とフィーチャーフラグを持つチームに最適です。
main (trunk)
│
├── short-lived feature (1-2 days max)
├── short-lived feature
└── short-lived feature
ルール:
全員が main または非常に短命なブランチにコミット
フィーチャーフラグで未完成の作業を隠蔽
マージ前に CI がパスする必要あり
1日に複数回デプロイ
GitFlow(複雑、リリースサイクル駆動)
スケジュールされたリリースやエンタープライズプロジェクトに最適です。
main (production releases)
│
└── develop (integration branch)
│
├── feature/user-auth
├── feature/payment
│
├── release/1.0.0 → merge to main and develop
│
└── hotfix/critical → merge to main and develop
# BAD: 曖昧でコンテキストがない
git commit -m "fixed stuff"
git commit -m "updates"
git commit -m "WIP"
# GOOD: 明確で具体的、理由を説明
git commit -m "fix(api): retry requests on 503 Service Unavailable
The external API occasionally returns 503 errors during peak hours.
Added exponential backoff retry logic with max 3 attempts.
Closes #123"
コミットメッセージテンプレート
リポジトリルートに .gitmessage を作成します:
# <type>(<scope>): <subject>
# # Types: feat, fix, docs, style, refactor, test, chore, perf, ci, revert
# Scope: api, ui, db, auth, etc.
# Subject: imperative mood, no period, max 50 chars
#
# [optional body] - explain why, not what
# [optional footer] - Breaking changes, closes #issue
有効化:git config commit.template .gitmessage
Merge vs Rebase
Merge(履歴を保持)
# Creates a merge commit
git checkout main
git merge feature/user-auth
# Result:# * merge commit# |\# | * feature commits# |/# * main commits
使用タイミング:
フィーチャーブランチを main にマージする場合
正確な履歴を保持したい場合
複数人がブランチで作業した場合
ブランチがプッシュ済みで他の人がベースにしている可能性がある場合
Rebase(直線的な履歴)
# Rewrites feature commits onto target branch
git checkout feature/user-auth
git rebase main
# Result:# * feature commits (rewritten)# * main commits
使用タイミング:
ローカルのフィーチャーブランチを最新の main で更新する場合
直線的でクリーンな履歴が欲しい場合
ブランチがローカルのみ(プッシュされていない)の場合
自分だけがブランチで作業している場合
Rebase ワークフロー
# Update feature branch with latest main (before PR)
git checkout feature/user-auth
git fetch origin
git rebase origin/main
# Fix any conflicts# Tests should still pass# Force push (only if you're the only contributor)
git push --force-with-lease origin feature/user-auth
<type>(<scope>): <description>
Examples:
feat(auth): add SSO support for enterprise users
fix(api): resolve race condition in order processing
docs(api): add OpenAPI specification for v2 endpoints
# Check for conflicts before merge
git checkout main
git merge feature/user-auth --no-commit --no-ff
# If conflicts, Git will show:# CONFLICT (content): Merge conflict in src/auth/login.ts# Automatic merge failed; fix conflicts and then commit the result.
コンフリクトの解決
# See conflicted files
git status
# View conflict markers in file# <<<<<<< HEAD# content from main# =======# content from feature branch# >>>>>>> feature/user-auth# Option 1: Manual resolution# Edit file, remove markers, keep correct content# Option 2: Use merge tool
git mergetool
# Option 3: Accept one side
git checkout --ours src/auth/login.ts # Keep main version
git checkout --theirs src/auth/login.ts # Keep feature version# After resolving, stage and commit
git add src/auth/login.ts
git commit
コンフリクト防止策
# 1. フィーチャーブランチを小さく短命に保つ# 2. main に対して頻繁に rebase する
git checkout feature/user-auth
git fetch origin
git rebase origin/main
# 3. 共有ファイルに触れる場合はチームとコミュニケーション# 4. 長期ブランチの代わりにフィーチャーフラグを使用# 5. PR を迅速にレビューしてマージ
# Delete local branches that are merged
git branch --merged main | grep -v "^\*\|main" | xargs -n 1 git branch -d
# Delete remote-tracking references for deleted remote branches
git fetch -p
# Delete local branch
git branch -d feature/user-auth # Safe delete (only if merged)
git branch -D feature/user-auth # Force delete# Delete remote branch
git push origin --delete feature/user-auth
Stash ワークフロー
# Save work in progress
git stash push -m "WIP: user authentication"# List stashes
git stash list
# Apply most recent stash
git stash pop
# Apply specific stash
git stash apply stash@{2}
# Drop stash
git stash drop stash@{0}
# 1. Add upstream remote (once)
git remote add upstream https://github.com/original/repo.git
# 2. Fetch upstream
git fetch upstream
# 3. Merge upstream/main into your main
git checkout main
git merge upstream/main
# 4. Push to your fork
git push origin main
ミスの取り消し
# Undo last commit (keep changes)
git reset --soft HEAD~1
# Undo last commit (discard changes)
git reset --hard HEAD~1
# Undo last commit pushed to remote
git revert HEAD
git push origin main
# Undo specific file changes
git checkout HEAD -- path/to/file
# Fix last commit message
git commit --amend -m "New message"# Add forgotten file to last commit
git add forgotten-file
git commit --amend --no-edit
Git フック
Pre-Commit フック
#!/bin/bash# .git/hooks/pre-commit# Run linting
npm run lint || exit 1
# Run tests
npm test || exit 1
# Check for secretsif git diff --cached | grep -E '(password|api_key|secret)'; thenecho"Possible secret detected. Commit aborted."exit 1
fi
Pre-Push フック
#!/bin/bash# .git/hooks/pre-push# Run full test suite
npm run test:all || exit 1
# Check for console.log statementsif git diff origin/main | grep -E 'console\.log'; thenecho"Remove console.log statements before pushing."exit 1
fi