| name | git-workflow |
| description | 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
ルール:
main は本番準備済みコードのみ
develop はインテグレーションブランチ
- フィーチャーブランチは
develop から作成、develop にマージ
- リリースブランチは
develop から作成、main と develop にマージ
- ホットフィックスブランチは
main から作成、main と develop の両方にマージ
どれを使うべきか
| 戦略 | チームサイズ | リリース頻度 | 最適な用途 |
|---|
| GitHub Flow | 任意 | 継続的 | SaaS、Web アプリ、スタートアップ |
| Trunk-Based | 5人以上の経験者 | 1日に複数回 | 高速チーム、フィーチャーフラグ |
| GitFlow | 10人以上 | スケジュール済み | エンタープライズ、規制産業 |
コミットメッセージ
Conventional Commits 形式
<type>(<scope>): <subject>
[optional body]
[optional footer(s)]
タイプ
| タイプ | 用途 | 例 |
|---|
feat | 新機能 | feat(auth): add OAuth2 login |
fix | バグ修正 | fix(api): handle null response in user endpoint |
docs | ドキュメント | docs(readme): update installation instructions |
style | フォーマット、コード変更なし | style: fix indentation in login component |
refactor | コードリファクタリング | refactor(db): extract connection pool to module |
test | テストの追加/更新 | test(auth): add unit tests for token validation |
chore | メンテナンスタスク | chore(deps): update dependencies |
perf | パフォーマンス改善 | perf(query): add index to users table |
ci | CI/CD の変更 | ci: add PostgreSQL service to test workflow |
revert | 前のコミットの取り消し | revert: revert "feat(auth): add OAuth2 login" |
良い例と悪い例
# 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(履歴を保持)
git checkout main
git merge feature/user-auth
使用タイミング:
- フィーチャーブランチを
main にマージする場合
- 正確な履歴を保持したい場合
- 複数人がブランチで作業した場合
- ブランチがプッシュ済みで他の人がベースにしている可能性がある場合
Rebase(直線的な履歴)
git checkout feature/user-auth
git rebase main
使用タイミング:
- ローカルのフィーチャーブランチを最新の
main で更新する場合
- 直線的でクリーンな履歴が欲しい場合
- ブランチがローカルのみ(プッシュされていない)の場合
- 自分だけがブランチで作業している場合
Rebase ワークフロー
git checkout feature/user-auth
git fetch origin
git rebase origin/main
git push --force-with-lease origin feature/user-auth
Rebase してはいけない場合
# 以下のブランチは絶対に rebase しないでください:
- 共有リポジトリにプッシュ済みのブランチ
- 他の人が作業のベースにしているブランチ
- 保護されたブランチ(main、develop)
- 既にマージ済みのブランチ
# 理由:Rebase は履歴を書き換え、他の人の作業を壊します
Pull Request ワークフロー
PR タイトル形式
<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
PR 説明テンプレート
## What
この PR が何をするかの簡潔な説明。
## Why
動機とコンテキストの説明。
## How
強調すべき主要な実装の詳細。
## Testing
- [ ] ユニットテストを追加/更新
- [ ] 統合テストを追加/更新
- [ ] 手動テストを実施
## Screenshots (if applicable)
UI 変更のビフォー/アフタースクリーンショット。
## Checklist
- [ ] プロジェクトのスタイルガイドラインに従っている
- [ ] セルフレビュー完了
- [ ] 複雑なロジックにコメントを追加
- [ ] ドキュメントを更新
- [ ] 新しい警告が発生しない
- [ ] ローカルでテストがパス
- [ ] 関連 issue をリンク
Closes #123
コードレビューチェックリスト
レビュアー向け:
作成者向け:
コンフリクト解決
コンフリクトの特定
git checkout main
git merge feature/user-auth --no-commit --no-ff
コンフリクトの解決
git status
git mergetool
git checkout --ours src/auth/login.ts
git checkout --theirs src/auth/login.ts
git add src/auth/login.ts
git commit
コンフリクト防止策
git checkout feature/user-auth
git fetch origin
git rebase origin/main
ブランチ管理
命名規則
# Feature branches
feature/user-authentication
feature/JIRA-123-payment-integration
# Bug fixes
fix/login-redirect-loop
fix/456-null-pointer-exception
# Hotfixes (production issues)
hotfix/critical-security-patch
hotfix/database-connection-leak
# Releases
release/1.2.0
release/2024-01-hotfix
# Experiments/POCs
experiment/new-caching-strategy
poc/graphql-migration
ブランチのクリーンアップ
git branch --merged main | grep -v "^\*\|main" | xargs -n 1 git branch -d
git fetch -p
git branch -d feature/user-auth
git branch -D feature/user-auth
git push origin --delete feature/user-auth
Stash ワークフロー
git stash push -m "WIP: user authentication"
git stash list
git stash pop
git stash apply stash@{2}
git stash drop stash@{0}
リリース管理
セマンティックバージョニング
MAJOR.MINOR.PATCH
MAJOR: 破壊的変更
MINOR: 新機能、後方互換性あり
PATCH: バグ修正、後方互換性あり
Examples:
1.0.0 → 1.0.1 (patch: bug fix)
1.0.1 → 1.1.0 (minor: new feature)
1.1.0 → 2.0.0 (major: breaking change)
リリースの作成
git tag -a v1.2.0 -m "Release v1.2.0
Features:
- Add user authentication
- Implement password reset
Fixes:
- Resolve login redirect issue
Breaking Changes:
- None"
git push origin v1.2.0
git tag -l
git tag -d v1.2.0
git push origin --delete v1.2.0
Changelog の生成
git log v1.1.0..v1.2.0 --oneline --no-merges
npx conventional-changelog -i CHANGELOG.md -s
Git の設定
必須設定
git config --global user.name "Your Name"
git config --global user.email "your@email.com"
git config --global init.defaultBranch main
git config --global pull.rebase true
git config --global push.default current
git config --global help.autocorrect 1
git config --global diff.algorithm histogram
git config --global color.ui auto
便利なエイリアス
[alias]
co = checkout
br = branch
ci = commit
st = status
unstage = reset HEAD --
last = log -1 HEAD
visual = log --oneline --graph --all
amend = commit --amend --no-edit
wip = commit -m "WIP"
undo = reset --soft HEAD~1
contributors = shortlog -sn
Gitignore パターン
# Dependencies
node_modules/
vendor/
# Build outputs
dist/
build/
*.o
*.exe
# Environment files
.env
.env.local
.env.*.local
# IDE
.idea/
.vscode/
*.swp
*.swo
# OS files
.DS_Store
Thumbs.db
# Logs
*.log
logs/
# Test coverage
coverage/
# Cache
.cache/
*.tsbuildinfo
よくあるワークフロー
新機能の開始
git checkout main
git pull origin main
git checkout -b feature/user-auth
git add .
git commit -m "feat(auth): implement OAuth2 login"
git push -u origin feature/user-auth
新しい変更で PR を更新
git add .
git commit -m "feat(auth): add error handling"
git push origin feature/user-auth
フォークとアップストリームの同期
git remote add upstream https://github.com/original/repo.git
git fetch upstream
git checkout main
git merge upstream/main
git push origin main
ミスの取り消し
git reset --soft HEAD~1
git reset --hard HEAD~1
git revert HEAD
git push origin main
git checkout HEAD -- path/to/file
git commit --amend -m "New message"
git add forgotten-file
git commit --amend --no-edit
Git フック
Pre-Commit フック
#!/bin/bash
npm run lint || exit 1
npm test || exit 1
if git diff --cached | grep -E '(password|api_key|secret)'; then
echo "Possible secret detected. Commit aborted."
exit 1
fi
Pre-Push フック
#!/bin/bash
npm run test:all || exit 1
if git diff origin/main | grep -E 'console\.log'; then
echo "Remove console.log statements before pushing."
exit 1
fi
アンチパターン
# BAD: main に直接コミット
git checkout main
git commit -m "fix bug"
# GOOD: フィーチャーブランチと PR を使用
# BAD: シークレットのコミット
git add .env # Contains API keys
# GOOD: .gitignore に追加し、環境変数を使用
# BAD: 巨大な PR(1000行以上)
# GOOD: 小さく焦点を絞った PR に分割
# BAD: "Update" のコミットメッセージ
git commit -m "update"
git commit -m "fix"
# GOOD: 説明的なメッセージ
git commit -m "fix(auth): resolve redirect loop after login"
# BAD: パブリック履歴の書き換え
git push --force origin main
# GOOD: パブリックブランチには revert を使用
git revert HEAD
# BAD: 長期間のフィーチャーブランチ(数週間/数ヶ月)
# GOOD: ブランチを短く保ち(数日)、頻繁に rebase
# BAD: 生成されたファイルのコミット
git add dist/
git add node_modules/
# GOOD: .gitignore に追加
クイックリファレンス
| タスク | コマンド |
|---|
| ブランチ作成 | git checkout -b feature/name |
| ブランチ切り替え | git checkout branch-name |
| ブランチ削除 | git branch -d branch-name |
| ブランチマージ | git merge branch-name |
| ブランチ rebase | git rebase main |
| 履歴表示 | git log --oneline --graph |
| 変更表示 | git diff |
| 変更ステージ | git add . or git add -p |
| コミット | git commit -m "message" |
| プッシュ | git push origin branch-name |
| プル | git pull origin branch-name |
| スタッシュ | git stash push -m "message" |
| 最後のコミットを取り消し | git reset --soft HEAD~1 |
| コミットを revert | git revert HEAD |