squash merge
在公共分支合并个人分支时,希望合并个人分支的多次commit时:
# git checkout dev
# git merge --squash feature
执行完命令后,squash merge不会产生提交,只是而是要手动add&commit,完成提交后的代码author就是当前用户的,而不是feature分支各个开发人员的。缺点就是后期问题追溯不好处理。
rebase merge
在开发中对一个小功能进行了3次提交,在合并到公开分支时要求去除个人分支中无用的提交,我们需要将commit其进行合并:
# git rebase -i HEAD~3
~
后面的数字代表提交的次数,执行完后会进入vi
编辑模式:
s 3e3e3f2 fix: minor typos in code
s d3960de refactor: avatar build
p 4goz8ec feat(user): add avatar
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
.
.
主要的两个参数是:
pick = use commit
squash = use commit, but meld into previous commit
根据提示参数来修改提交记录,上述中我们只保留commit4goz8ec
,而3e3e3f2
和d3960de
我们将这两个commit合并到上一个commit中,编辑完成后保存即可。
保存完后会出现一个新的编辑界面,这个界面让我们输入新的commit message
feat(user): add avatar
# This is a combination of 2 commits.
# The first commit's message is:
# 这里将其注释即可
# refactor: avatar build
#
# This is the 2nd commit message:
# 这里将其注释即可
# fix: minor typos in code
#
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# Not currently on any branch.
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
我们可以更改新的提交,后续的两次commit进行注释或删除,上述中代码保留原有提交只对无用commit进行注释,之后再次保存就能去除无用的commit了。当然如果不保存而是直接退出的话,新的commit就会变成多次提交的结果:
feat(user): add avatar
refactor: avatar build
fix: minor typos in code
合并完成后再切换到公开分支进行merge
总结
squash
会统一author并且保留所有的commit,有些公开分支要求只有一条commit line时推荐还是在个人分支中rebase
,既能保留author也能在合并时形成一条commit line。当然不熟悉的情况下出现问题的情况也比较多,处理起来也麻烦。
网友评论