首页·终端命令

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