在合並分支的時候,希望將多次提交合並成一個,然后再 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
這樣我們再推送到遠程就可以實現合並更新了。