git merge コマンドは、現在のブランチに別のブランチを統合(マージ)するためのコマンドです。
複数の開発ラインをまとめ、機能追加や修正を取り込むときに利用されます。
構文(Syntax)
git merge [オプション] <ブランチ名>
主なオプション一覧
| オプション | 説明 | 使用例 |
|---|---|---|
<ブランチ名> | 指定ブランチを現在のブランチにマージ | git merge feature-x |
--no-ff | Fast-forward を無効化し、必ずマージコミットを作成 | git merge --no-ff feature-x |
--ff-only | Fast-forward 可能な場合のみマージを実行 | git merge --ff-only feature-x |
--squash | 変更をまとめて 1 コミットにする(コミットは別途必要) | git merge --squash feature-x |
--abort | コンフリクトが発生した場合にマージを中止 | git merge --abort |
-m "メッセージ" | マージコミットにメッセージを指定 | git merge -m "Merge feature-x" |
実行例
基本的なマージ
git checkout main
git merge feature-x
出力例:
Updating abc123..def456
Fast-forward
file1.txt | 2 ++
1 file changed, 2 insertions(+)
マージコミットを必ず作成
git merge --no-ff feature-x
出力例:
Merge made by the 'recursive' strategy.
file2.txt | 5 +++++
Fast-forward のみ許可
git merge --ff-only hotfix
Squash マージ(1コミットにまとめる)
git merge --squash feature-login
git commit -m "Squash merge feature-login"
コンフリクトが発生した場合
git merge feature-y
出力例:
Auto-merging app.py
CONFLICT (content): Merge conflict in app.py
Automatic merge failed; fix conflicts and then commit the result.
解決後:
git add app.py
git commit
マージを中止
git merge --abort
エラー例(存在しないブランチを指定)
git merge notfound
出力例:
fatal: not something we can merge in: notfound
関連コマンド
git rebase: マージの代わりに履歴を付け替えて統合する。git checkout/git switch: マージ先ブランチに切り替える。git log --graph: ブランチの統合履歴を確認する。
備考
- Fast-forward マージでは新しいコミットは作成されず、ブランチの先頭が進むだけ。
--no-ffを指定すると履歴が分かりやすくなるが、コミット数は増える。- コンフリクト解決はマージ作業で最も重要な手順の一つ。
参考
- Git公式ドキュメント: https://git-scm.com/docs/git-merge

コメント