Git Workflow完全ガイド
Git Workflow完全ガイド
Section titled “Git Workflow完全ガイド”実践的なGit Workflowを、シナリオ別に詳しく解説します。
なぜGit Workflowを理解する必要があるのか
Section titled “なぜGit Workflowを理解する必要があるのか”Workflowなしの問題
Section titled “Workflowなしの問題”問題のある状況:
開発者A: 「どのブランチで作業すればいいの?」開発者B: 「リリースはどうやって管理するの?」開発者C: 「緊急修正はどうすればいいの?」
// 問題点:// - ブランチの使い方が統一されていない// - リリース管理が困難// - 緊急対応が困難// - チーム間の混乱影響:
- 開発効率の低下
- リリース管理の困難
- 緊急対応の困難
- チーム間の混乱
Git Workflowによる解決
Section titled “Git Workflowによる解決”改善された状況:
統一されたWorkflow:- ブランチの使い方が明確- リリース管理が容易- 緊急対応が明確- チーム間の協力が向上メリット:
- 開発効率の向上
- リリース管理の容易化
- 緊急対応の明確化
- チーム間の協力向上
Git Flowの詳細なワークフロー
Section titled “Git Flowの詳細なワークフロー”# 1. リポジトリをクローンgit clone https://github.com/example/project.gitcd project
# 2. developブランチを作成git checkout -b develop maingit push -u origin develop
# 3. ブランチの保護設定(GitHub/GitLab)# - mainブランチ: 直接プッシュを禁止、プルリクエスト必須# - developブランチ: 直接プッシュを禁止、プルリクエスト必須機能開発のワークフロー
Section titled “機能開発のワークフロー”完全な流れ:
# 1. 最新のdevelopブランチを取得git checkout developgit pull origin develop
# 2. featureブランチを作成git checkout -b feature/user-authentication develop
# 3. 開発作業git add src/auth/login.tsgit commit -m "feat: ログイン機能を実装"
git add src/auth/logout.tsgit commit -m "feat: ログアウト機能を実装"
git add tests/auth.test.tsgit commit -m "test: 認証機能のテストを追加"
# 4. 定期的にdevelopブランチの変更を取り込むgit checkout developgit pull origin developgit checkout feature/user-authenticationgit merge develop# またはgit rebase develop
# 5. リモートにプッシュgit push -u origin feature/user-authentication
# 6. プルリクエストを作成(developブランチへのマージ)# - タイトル: feat: ユーザー認証機能を実装# - 説明: 実装内容、テスト方法、関連Issue# - レビュアーを指定
# 7. レビューと承認
# 8. developブランチにマージ(プルリクエスト経由)
# 9. ローカルのdevelopブランチを更新git checkout developgit pull origin develop
# 10. featureブランチを削除git branch -d feature/user-authenticationgit push origin --delete feature/user-authenticationリリースのワークフロー
Section titled “リリースのワークフロー”完全な流れ:
# 1. リリース準備の開始git checkout developgit pull origin develop
# 2. releaseブランチを作成git checkout -b release/v1.0.0 develop
# 3. バージョン番号を更新# package.json、CHANGELOG.mdなどを更新git add package.json CHANGELOG.mdgit commit -m "chore: バージョン1.0.0の準備"git push -u origin release/v1.0.0
# 4. リリース前のバグ修正git add .git commit -m "fix: リリース前のバグ修正"git push origin release/v1.0.0
# 5. ステージング環境でテスト
# 6. mainブランチにマージgit checkout maingit pull origin maingit merge --no-ff release/v1.0.0git tag -a v1.0.0 -m "リリース v1.0.0"git push origin main --tags
# 7. developブランチにもマージgit checkout developgit pull origin developgit merge --no-ff release/v1.0.0git push origin develop
# 8. releaseブランチを削除git branch -d release/v1.0.0git push origin --delete release/v1.0.0
# 9. 本番環境にデプロイホットフィックスのワークフロー
Section titled “ホットフィックスのワークフロー”完全な流れ:
# 1. 緊急バグが報告された# バグ: セキュリティ脆弱性
# 2. mainブランチからhotfixブランチを作成git checkout maingit pull origin maingit checkout -b hotfix/security-patch main
# 3. 緊急修正git add .git commit -m "hotfix: セキュリティパッチを適用"git push -u origin hotfix/security-patch
# 4. テストを実行npm test
# 5. mainブランチにマージgit checkout maingit merge --no-ff hotfix/security-patchgit tag -a v1.0.1 -m "ホットフィックス v1.0.1"git push origin main --tags
# 6. developブランチにもマージgit checkout developgit pull origin developgit merge --no-ff hotfix/security-patchgit push origin develop
# 7. hotfixブランチを削除git branch -d hotfix/security-patchgit push origin --delete hotfix/security-patch
# 8. 本番環境にデプロイGitHub Flowの詳細なワークフロー
Section titled “GitHub Flowの詳細なワークフロー”基本的なワークフロー
Section titled “基本的なワークフロー”完全な流れ:
# 1. mainブランチを最新化git checkout maingit pull origin main
# 2. featureブランチを作成git checkout -b feature/new-feature
# 3. 開発作業git add .git commit -m "feat: 新機能を実装"git push -u origin feature/new-feature
# 4. プルリクエストを作成(GitHubのWebインターフェース)# - タイトル: feat: 新機能を実装# - 説明: 実装内容、テスト方法# - レビュアーを指定# - ラベルを設定
# 5. CI/CDの実行を確認
# 6. レビューと承認
# 7. mainブランチにマージ(GitHubのWebインターフェース)# - "Squash and merge" または "Rebase and merge" を選択
# 8. mainブランチを更新git checkout maingit pull origin main
# 9. featureブランチを削除git branch -d feature/new-featuregit push origin --delete feature/new-featureプルリクエストのベストプラクティス
Section titled “プルリクエストのベストプラクティス”プルリクエストのテンプレート:
## 変更内容- ユーザー認証機能を実装- ログイン・ログアウト機能を追加
## 関連IssueCloses #123
## チェックリスト- [ ] コードレビューを依頼した- [ ] テストを追加した- [ ] ドキュメントを更新した- [ ] CIが成功している- [ ] 破壊的変更がない(あれば説明を追加)
## スクリーンショット(UI変更の場合)[スクリーンショットを添付]
## テスト方法1. ログイン画面にアクセス2. ユーザー名とパスワードを入力3. 「ログイン」ボタンをクリック4. ダッシュボードに遷移することを確認GitLab Flowの詳細なワークフロー
Section titled “GitLab Flowの詳細なワークフロー”環境ごとのブランチ
Section titled “環境ごとのブランチ”完全な流れ:
# 1. featureブランチで開発git checkout -b feature/new-feature maingit add .git commit -m "feat: 新機能を実装"git push -u origin feature/new-feature
# 2. mainブランチにマージ(プルリクエスト経由)
# 3. pre-productionブランチにマージ(ステージング環境)git checkout pre-productiongit pull origin pre-productiongit merge maingit push origin pre-production
# 4. ステージング環境でテスト
# 5. productionブランチにマージ(本番環境)git checkout productiongit pull origin productiongit merge pre-productiongit push origin production
# 6. 本番環境にデプロイTrunk-based Developmentの詳細なワークフロー
Section titled “Trunk-based Developmentの詳細なワークフロー”基本的なワークフロー
Section titled “基本的なワークフロー”完全な流れ:
# 1. mainブランチを最新化git checkout maingit pull origin main
# 2. 短命なfeatureブランチを作成git checkout -b feature/small-change
# 3. 迅速に開発(1-2日以内)git add .git commit -m "feat: 小さな変更"git push -u origin feature/small-change
# 4. すぐにプルリクエストを作成
# 5. レビューと承認
# 6. mainブランチにマージgit checkout maingit pull origin main
# 7. featureブランチを削除git branch -d feature/small-changegit push origin --delete feature/small-change大規模な機能開発
Section titled “大規模な機能開発”完全な流れ:
# 1. featureブランチを作成git checkout -b feature/large-feature main
# 2. 小さなコミットを頻繁に作成git add src/feature/part1.tsgit commit -m "feat: 機能の一部を実装"git push origin feature/large-feature
git add src/feature/part2.tsgit commit -m "feat: 機能の別の部分を実装"git push origin feature/large-feature
# 3. 定期的にmainブランチを取り込むgit checkout maingit pull origin maingit checkout feature/large-featuregit merge main# またはgit rebase main
# 4. 完了したらプルリクエストを作成
# 5. レビューと承認
# 6. mainブランチにマージ実践的なシナリオ
Section titled “実践的なシナリオ”シナリオ1: 新機能の開発
Section titled “シナリオ1: 新機能の開発”# 1. 要件を確認# 2. featureブランチを作成git checkout -b feature/payment-integration develop
# 3. 開発作業git add .git commit -m "feat: 決済機能を実装"
# 4. テストを追加git add tests/payment.test.tsgit commit -m "test: 決済機能のテストを追加"
# 5. developブランチにマージ(プルリクエスト経由)シナリオ2: バグ修正
Section titled “シナリオ2: バグ修正”# 1. バグを報告された# 2. bugfixブランチを作成git checkout -b bugfix/login-error develop
# 3. バグを修正git add .git commit -m "fix: ログインエラーを修正"
# 4. テストを追加git add tests/login.test.tsgit commit -m "test: ログインエラーのテストを追加"
# 5. developブランチにマージ(プルリクエスト経由)シナリオ3: 緊急修正
Section titled “シナリオ3: 緊急修正”# 1. 緊急バグが報告された# 2. hotfixブランチを作成git checkout -b hotfix/critical-bug main
# 3. 緊急修正git add .git commit -m "hotfix: 緊急バグを修正"
# 4. mainブランチにマージgit checkout maingit merge --no-ff hotfix/critical-buggit tag -a v1.0.1 -m "ホットフィックス v1.0.1"git push origin main --tags
# 5. developブランチにもマージgit checkout developgit merge --no-ff hotfix/critical-buggit push origin developGit Workflow完全ガイドのポイント:
- Git Flow: 複数のブランチタイプ、明確な役割分担、リリース管理
- GitHub Flow: シンプルなワークフロー、プルリクエスト中心
- GitLab Flow: 環境ごとのブランチ、デプロイの制御
- Trunk-based Development: 短命なブランチ、迅速な開発
- 実践的なシナリオ: 新機能の開発、バグ修正、緊急修正
適切なGit Workflowを実践することで、効率的で安全な開発ができます。