美文网首页
git 合并多余的commit

git 合并多余的commit

作者: JJNile | 来源:发表于2019-05-11 20:27 被阅读0次

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,而3e3e3f2d3960de我们将这两个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。当然不熟悉的情况下出现问题的情况也比较多,处理起来也麻烦。

相关文章

  • git 合并多余的commit

    squash merge 在公共分支合并个人分支时,希望合并个人分支的多次commit时: 执行完命令后,squa...

  • git 简单粗暴合并几个commit 分支相当干净

    git 简单粗暴合并几个commit

  • git 合并指定commit

    合并单个commit到指定的分支上 cherry pick 合并单个 commit 具体操作: git log /...

  • git 基本操作

    合并代码 git add . git commit -m "修改了XXX" git pull origin mas...

  • ios 开发小方法

    1.git cherry-pick合并某个commit

  • merge时 source 落后

    先把本地新增的文件commit之后再去合并远程仓库的,具体操作如下:git add .git commit -m ...

  • git commit合并

    git rebase -i 合并之前的那个commit pick表示执行squash表示被合并 pick命令保留要...

  • git commit 合并

    背景 开发过程中可能有多个commit,直接合并到master分支会让master分支的git log看起来非常错...

  • git 合并commit

    problem 团队开发过程中可能需要提交merge request相互code review,但是每次merge...

  • git commit合并

    如何修改commit common git commit --amend之后使用vim命令正常修改 如何将当前提交...

网友评论

      本文标题:git 合并多余的commit

      本文链接:https://www.haomeiwen.com/subject/lpefaqtx.html