跳轉至

建立 2024-01-01 更新 2026-09-15

git merge

git merge 把另一條分支的歷史合進目前所在的分支。通常你先 git switch main,再合併功能分支。GitHub 上按 Merge pull request,遠端做的也是同一件事。

基本用法

git switch main
git merge feature/new-feature

能快轉(fast-forward)時,main 只是往前移;不能快轉時會多一個 merge commit。

遇到衝突

兩邊改了同一段,檔案裡會出現:

<<<<<<< HEAD
這是 main 分支的內容
=======
這是 feature/new-feature 分支的內容
>>>>>>> feature-branch

刪掉標記、留下正確內容,然後:

git add <file>
git commit

檢查結果

git log --graph --oneline

合併前

先 git pull 讓目前分支跟上遠端,再 merge,比較少一次衝突。不想留下 merge commit、要把自己的提交接到最新 main 上,才考慮較進階的 git rebase。