首頁·終端機命令

git merge

git

用法

git merge [--no-ff|--squash] <branch>

把另一個分支的工作併進目前分支;--squash 不會留下合併記錄,所以之後 git 仍然認為那個分支沒被合併。

常用選項

選項含義
--no-ffalways record a merge commit, even when a fast-forward was possible
--ff-onlyrefuse unless it can fast-forward
--squashstage the combined change without committing or recording a merge
--abortundo a merge that stopped on conflicts
--continuefinish after you resolved the conflicts
-X ours|theirsauto-resolve conflicting hunks one way
--no-commitmerge but leave the commit to you

範例

git merge --no-ff feature/login

keeps the branch visible in history

git merge --abort

gets you back to before the conflicted merge

git merge --squash feature/login

one flat change, staged; you still have to commit

難的地方在於復原有好幾種:把分支的指向挪走,和補一個反向提交,不是一回事。

怎麼看

  • 方括號 [ ] 表示這部分可以不寫。
  • 省略號 … 表示可以寫多個。
  • 選項區分大小寫——有些命令裡 -r 和 -R 不是一回事。

常見問題

Q. git merge 是做什麼的?

把另一個分支的工作併進目前分支;--squash 不會留下合併記錄,所以之後 git 仍然認為那個分支沒被合併。

Q. 怎麼寫?

git merge [--no-ff|--squash] <branch> —— 方括號表示可以省略的部分。

Q. 值得記的選項有幾個?

這裡列了 7 個,完整列表在 man git。這條命令屬於git。

相關命令

man 手冊: man git-merge