美文网首页
Git 合并多次提交

Git 合并多次提交

作者: zucchiniy | 来源:发表于2020-01-10 17:55 被阅读0次

在合并分支的时候,希望将多次提交合并成一个,然后再 cherry-pick 到主分支。

合并分支

develop 分支做开发,可能会进行多次提交,但是在发布或者进行 PR 的时候,我们只希望看到一次提交。这个时候,我们需要进行 git rebase 之后进行合并。

# HEAD~3 表示将近三次提交都合并,如果是将 2 次合并则为 HEAD~2
git rebase -i HEAD~3

这个时候,看到的是一上对 COMMIT 信息的提示

pick 9ba5122 2017 年 8 月 2 日
pick c6da035 ~~

# Rebase 9b6bae1..c6da035 onto 9b6bae1 (2 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

第一列对应的是 rebase 具体的操作,其含义如下

命令 作用
pick(p) git 会应用这个补丁,以同样的提交信息(commit message)保存提交
reword(r) git 会应用这个补丁,但需要重新编辑提交信息
edit(e) git 会应用这个补丁,但会因为 amending 而终止
squash(s) git 会应用这个补丁,但会与之前的提交合并
fixup(f) git 会应用这个补丁,但会丢掉提交日志
exec(x) git 会在 shell 中运行这个命令
drop(d) git 会移除这次 COMMIT

将第二个 pick c6da035 ~~~ 这一行修改成 squash c6da035 ~~~ ,使之与之前的提交合并。

保存之后可以看到下面的内容

This is a combination of 2 commits.
# This is the 1st commit message:

2017 年 8 月 2 日

删除无用配置,提高启动速度

1. 更新 zucchini-org
2. 增加 CHANGELOG 用来记录每次更新
3. 更新 plantuml 配置
   FIXED Can't find plantuml-jar-path
4. 增加 parinfer 配置,用来优化 lisp 的编写速度

# This is the commit message #2:

~~

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Tue Aug 1 10:24:44 2017 +0800
#
# interactive rebase in progress; onto 9b6bae1
# Last commands done (2 commands done):
"~/spacemacs/spacemacs.d/.git/COMMIT_EDITMSG" 36L, 1003C

修改成正确的 commit 信息之后,保存存并退出,可以看到下面的内容

$ git rebase -i HEAD~2
[detached HEAD 0238691] 2017 年 8 月 2 日
 Date: Tue Aug 1 10:24:44 2017 +0800
 5 files changed, 65 insertions(+), 34 deletions(-)
 create mode 100644 CHANGELOG.org
 rewrite local/custom.el (66%)
Successfully rebased and updated refs/heads/develop.

这个时候,就已经将我们这几次的更改都合并到一次中了。

cherry-pick 分支并更新

这个时候,就可以更新我们的代码了。

首先 git checkout master 分支, 然后更新我们的代码 git pull

然后将我们合并之后的 develop 分支的内容更新过来

git log -b develop

看到如下内容

commit 02386914b9e5ab13c23451a3463813bfdecb157a
Author: 语乱 <banshiliuli1990@sina.com>
Date:   Tue Aug 1 10:24:44 2017 +0800

    2017 年 8 月 2 日

    删除无用配置,提高启动速度

    1. 更新 zucchini-org
    2. 增加 CHANGELOG 用来记录每次更新
    3. 更新 plantuml 配置
       FIXED Can't find plantuml-jar-path
    4. 增加 parinfer 配置,用来优化 lisp 的编写速度

或者使用上次的操作的中的提示 [detached HEAD 0238691] 2017 年 8 月 2 日 其中的 0238691 就是我们需要

git cherry-pick 0238691

这样我们再推送到远程就可以实现合并更新了。

文章首发于:https://www.zucchiniy.cn

相关文章

  • git 一些用法: 合并,修改提交信息,回退版本

    1. 合并多次提交 1) 查看修改历史 git log 2) 合并6次提交git rebase -i HEAD~6...

  • Git合并多次提交

    rebase简介 rebase的作用简要概括为:可以对某一段线性提交历史进行编辑、删除、复制、粘贴;因此,合理使用...

  • Git合并多次提交

    我们将自己的分支合并到主分支的时候,都希望将自己的多次修改bug和调试的commit合并为一次功能commit,然...

  • [Git]合并多次提交

    1 问题提出 很多时候,很难作到一次提交能够完整地解决一个功能,经常性出现以下情况:为了完成2个功能,一共进行了5...

  • Git 合并多次提交

    在合并分支的时候,希望将多次提交合并成一个,然后再 cherry-pick 到主分支。 合并分支 develop ...

  • 七、git合并多次提交

    在开发中我们需要保持远程仓库清洁,不希望本地开发多次提交信息都提交到远程仓库; 第一种方法:git rebase ...

  • git合并多次提交为一次提交

    在实际开发工作中,很少有一次提交git就达成目标的情况,往往是需要多次修改提交,那么多次提交git,在合并到mas...

  • Git rebase 使用记录

    当你想把git 的多次提交记录合并为一次时,可以使用git rebase.1、先查历史提交记录 2、git reb...

  • 补充2Git命令知多少

    6.git rebase 应用场景: 1)合并多次提交信息: A.在一个版本库中提交了三此commit: B.合并...

  • 如何使用git合并多次提交

    在为代码添加一个新功能的时候你会怎么做?(从git的操作顺序来说) 如果是我的话,顺序如下: 先是使用git ch...

网友评论

      本文标题:Git 合并多次提交

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