Git 合併屢次提交

在合併分支的時候,但願將屢次提交合併成一個,而後再 cherry-pick 到主分支。git

合併分支

develop 分支作開發,可能會進行屢次提交,可是在發佈或者進行 PR 的時候,咱們只但願看到一次提交。這個時候,咱們須要進行 git rebase 以後進行合併。shell

# 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 具體的操做,其含義以下this

命令 做用
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 ~~~ ,使之與以前的提交合並。spa

保存以後能夠看到下面的內容rest

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.

這個時候,就已經將咱們這幾回的更改都合併到一次中了。code

cherry-pick 分支並更新

這個時候,就能夠更新咱們的代碼了。開發

首先 git checkout master 分支, 而後更新咱們的代碼 git pullrem

而後將咱們合併以後的 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

相關文章
相關標籤/搜索