Git: Gitのブランチ管理と並行開発
ブランチは、Gitの最も強力な機能の一つです。ブランチを使えば、チームは互いに影響を与えることなく、異なる機能を並行して開発し、後でそれらをマージすることができます。Gitでのブランチの作成は非常に簡単であるため、ブランチを頻繁に活用するよう促されます。
1. ブランチの基本概念
(1) ブランチとは何か?
Gitにおいて、ブランチとは特定のコミットを指す移動可能なポインタのことです。コミットが行われるたびに、現在のブランチポインタは自動的に新しいコミットを指すように移動します。
graph LR
C1[SubmitC1] --> C2[SubmitC2] --> C3[SubmitC3]
main[mainBranch] --> C3
HEAD[HEAD] --> main
style main fill:#d4edda
style HEAD fill:#fff3cd
(2) 枝の本質
Gitのブランチとは、本質的には41バイトのデータを含むファイルのことです:
- 40バイトのSHA-1ハッシュ値
- 1バイトの改行文字
だからこそ、Gitはこれほど素早くブランチを作成できるのです。
BASH
# View Branch Files
cat .git/refs/heads/main
# Output:
# a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0
(3) HEADポインタ
HEAD は、現在の分岐を指す特別なポインタです:
graph TB
HEAD[HEAD] --> main[mainBranch]
main --> C3[SubmitC3]
C3 --> C2[SubmitC2]
C2 --> C1[SubmitC1]
style HEAD fill:#f8d7da
style main fill:#d4edda
(4) 支店のメリット
- 並行開発:複数の人が異なるブランチで同時に作業すること
- 機能の分離:新機能の開発がメインブランチに影響を与えない
- 安全な実験:ブランチ上で実験を行い、失敗した場合はそのブランチを削除する
- バージョン管理:異なるバージョンは、それぞれ別のブランチで管理されています
2. ブランチを表示する
(1) ローカルブランチを表示する
▶ サンプル:ブランチの一覧表示
BASH
# View local branches
git branch
# Output:
# * main
# develop
# feature
# * Indicates the current branch
# View Branches and Last Commit
git branch -v
# Output:
# * main a1b2c3d feat: Add a login feature
# develop d4e5f6g Fix: FixBug
# feature h7i8j9k WIP: New Features
# View detailed branch information
git branch -vv
# The output includes information about the upstream branch:
# * main a1b2c3d [origin/main] feat: Add a login feature
(2) すべてのブランチを表示
▶ サンプル:リモートブランチの表示
BASH
# View All Branches(Local+Remote)
git branch -a
# Output:
# * main
# develop
# remotes/origin/HEAD -> origin/main
# remotes/origin/main
# remotes/origin/develop
# remotes/origin/feature
# View only remote branches
git branch -r
# Output:
# origin/HEAD -> origin/main
# origin/main
# origin/develop
# origin/feature
(3) ブランチ間の関係を確認する
▶ サンプル:分岐のグラフィカル表示
BASH
# View the branch history graph
git log --oneline --graph --all
# Output:
# * a1b2c3d (HEAD -> main) feat: Add Login
# | * d4e5f6g (feature) WIP: New Features
# |/
# * h7i8j9k Initial commit
# View the commits included in a branch
git log --oneline --graph --decorate --all
# View Branch Fork Points
git merge-base main feature
3. ブランチを作成する
(1) 新しいブランチを作成する
▶ サンプル:ブランチの作成
BASH
# Create a Branch(Based on the current commit)
git branch feature
# Create a branch and specify its starting point
git branch feature a1b2c3d
# Create a branch based on a specific tag
git branch v1.1-branch v1.0.0
# Create a branch and switch to it immediately
git checkout -b feature
# New Grammar(Recommendations)
git switch -c feature
# Created from a remote branch
git checkout -b feature origin/feature
# Or
git switch -c feature origin/feature
(2) ブランチ作成のワークフロー
graph TB
A[mainBranch] --> B[git branch feature]
B --> C[New Branchfeature]
C --> D[feature: Go to Development]
D --> E[Submit Changes]
E --> F[Merge back intomain]
style A fill:#d4edda
style C fill:#fff3cd
style F fill:#c3e6cb
(3) ブランチの命名規則
▶ サンプル:標準的なブランチの命名規則
TEXT
📖 参照専用
Feature Branch:feature/user-auth
feature/shopping-cart
feature/payment-integration
Fix Branch:fix/login-error
fix/memory-leak
Thermal Repair Branch:hotfix/critical-security-issue
hotfix/payment-failure
Release Branch:release/v1.0.0
release/v2.1.0
Development Branch:develop
Main Branch: main (or master)
4. ブランチを切り替える
(1) ブランチを切り替えるコマンド
▶ サンプル:ブランチの切り替え
BASH
# Traditional Commands
git checkout feature
# New Command(Recommendations)
git switch feature
# Switch to the previous branch
git switch -
# Switch tomainBranch
git switch main
# Create and Switch
git switch -c new-feature
# Output:
# Switched to branch 'new-feature'
(2) ブランチ切り替えの仕組み
graph TB
A[Before switching branches] --> B[UpdateHEADPointer]
B --> C[Update the temporary storage area]
C --> D[Update Workspace]
D --> E[Switching complete]
style A fill:#fff3cd
style E fill:#d4edda
ブランチを切り替える際、Git は以下の処理を行います:
- HEAD を更新して、新しいブランチを指すようにする
- 新しいブランチの状態を反映するようにステージング領域を更新する
- ワークスペース内のファイルを、新しいブランチの内容で更新する
(3) 保存されていない変更の処理
▶ サンプル:ブランチを切り替える前に変更を処理する
BASH
# View Current Status
git status
# Method1:Submit Changes
git add .
git commit -m "WIP: Save as Draft"
git switch feature
# Method2:Save Changes Temporarily(stash)
git stash
git switch feature
# After completing other tasks
git switch main
git stash pop
# Method3:Cancel Edits
git restore .
git switch feature
# Method4:Carry, Modify, Toggle(If there is no conflict)
git switch feature
# If the changes do not conflict with the new branch,Changes will be saved
5. ブランチの削除
(1) マージされたブランチを削除する
▶ サンプル:ブランチを安全に削除する
BASH
# Delete Merged Branches
git branch -d feature
# Output:
# Deleted branch feature (was a1b2c3d).
# Delete Multiple Branches
git branch -d feature1 feature2
# Delete a merged remote branch
git push origin --delete feature
# Or
git push origin :feature
(2) ブランチの強制削除
▶ サンプル:マージされていないブランチを強制的に削除する
BASH
# Try deleting the unmerged branch
git branch -d feature
# Output Error:
# error: The branch 'feature' is not fully merged.
# If you are sure you want to delete it, run 'git branch -D feature'.
# Forced Deletion
git branch -D feature
# Output:
# Deleted branch feature (was a1b2c3d).
(3) リモートブランチの整理
▶ サンプル:削除されたリモートブランチのクリーンアップ
BASH
# View remote branch references
git branch -r
# Delete a local reference to a remote branch(Remotely Deleted)
git fetch -p
# Or
git remote prune origin
# View which remote branch references need to be cleaned up
git remote prune origin --dry-run
6. 支店運営のベストプラクティス
(1) 支店戦略
一般的な分岐戦略:
Git Flow:
graph TB
main[main Production] --> release[release Published]
release --> develop[develop Development]
develop --> feature[feature Features]
main --> hotfix[hotfix Thermal Repair]
style main fill:#d4edda
style develop fill:#fff3cd
style feature fill:#e1f5ff
style hotfix fill:#f8d7da
GitHub Flow:
- メインブランチは常にデプロイ可能です
mainから機能ブランチを作成する- 開発が完了したら、プルリクエストを作成してください
- レビュー後にmainブランチにマージする
(2) ブランチ名の付け方に関する推奨事項
▶ サンプル:ブランチの命名規則
BASH
# Feature Branch
feature/user-authentication
feature/shopping-cart
feature/payment-integration
# BugFix
fix/login-validation-error
fix/memory-leak-issue
# Thermal Repair
hotfix/security-vulnerability
hotfix/critical-bug
# Published
release/v1.0.0
release/v2.1.0
# Experiment
experiment/new-architecture
spike/performance-optimization
(3) ブランチのワークフロー
▶ サンプル:ブランチのワークフロー全体
BASH
# 1. Create a feature branch from main
git checkout main
git pull origin main
git checkout -b feature/user-auth
# 2. Developing on a feature branch
echo "auth code" > auth.js
git add auth.js
git commit -m "feat: Add User Authentication"
# 3. Stay in touch withmainSynchronize
git fetch origin
git merge origin/main
# or userebase
git rebase origin/main
# 4. Push Feature Branch
git push -u origin feature/user-auth
# 5. Create Pull Request (on GitHub)
# 6. Delete feature branches after merging
git checkout main
git pull origin main
git branch -d feature/user-auth
git push origin --delete feature/user-auth
❓ よくある質問
Q 新しいブランチを作成した後、元のブランチに影響はありますか?
A いいえ。ブランチを作成しても、単に現在のコミットを指す新しいポインタが作成されるだけであり、元のブランチにはまったく影響しません。2つのブランチは独立して発展させることができます。
Q あるブランチがどのブランチから作成されたかを確認するにはどうすればよいですか?
A
git log --oneline --graph --all を使用するとブランチの履歴グラフを表示でき、git merge-base <branch1> <branch2> を使用すると 2 つのブランチの分岐点を表示できます。Q ブランチを削除すると、コミットは失われてしまいますか?
A コミットが別のブランチから参照されている場合(例えば、
main にマージされている場合など)、そのブランチを削除しても、そのコミットが失われることはありません。削除対象のブランチにのみ存在するコミットは、削除時に失われますが、git reflog を使用して復元することができます。Q 「checkout」と「switch」の違いは何ですか?
A
git switch は、Git 2.23 で導入された新しいコマンドで、ブランチの切り替えに特化しており、より明確な意味論を備えています。git checkout は、ブランチの切り替え、ファイルの復元、ブランチの作成など、より包括的な機能を提供します。ブランチを切り替える際には、git switch を使用することをお勧めします。Q ブランチ間の違いを比較するにはどうすればよいですか?
A
git diff main feature を使用して2つのブランチの違いを比較し、git diff main...feature を使用してメインブランチに対する機能ブランチの変更内容を確認します。📖 まとめ
- ブランチとは、コミットへの可変ポインタであり、その作成にかかるコストは極めて低い。
- ブランチを表示:
git branchでローカルブランチを一覧表示、-aで全ブランチを表示 - ブランチ
git branch <name>またはgit switch -c <name>を作成し、そのブランチを作成して切り替える - ブランチを切り替える:
git switch <name>HEAD および作業ディレクトリを更新する - ブランチの削除:
git branch -dマージ済みのブランチを削除;-D強制削除 - ブランチ戦略:Git Flow、GitHub Flowなど。チームに最適な戦略を選択してください。
📝 練習問題
-
基本演習:Gitリポジトリを作成し、複数のブランチ(main、develop、feature)を作成します。各ブランチに異なる変更をコミットし、
git branchおよびgit log --graphを使用してブランチ構造を確認してください。 -
応用演習:分岐ワークフロー全体をシミュレートします。
mainから機能ブランチを作成し、その機能を開発し、mainと同期を保ち、最後にmainにマージして、機能ブランチを削除します。 -
課題:
develop、feature、release、hotfixといったブランチを作成し、実際のプロジェクトで Git Flow のブランチ戦略を実装して、ブランチ管理の全プロセスを体験してください。