1. 程式人生 > >git 操作之合併commit(提交之前本地合併)

git 操作之合併commit(提交之前本地合併)

本篇文章主要講的是git操作之合併同一個分支的不同提交資訊,即將多個提交記錄合併為一個。
這裡主要是使用“git rebase”命令,推薦在未提交到遠端倉庫的時候修改本地記錄使用。

步驟:

一、首 先要切換的要合併commit的分支
二、然後使用命令“git rebase -i HEAD~2”(後面的2代表著要合併的分支數量),最後出來的介面如下:

pick c6e4557 create second.txt
pick e1a7dfa add text in second.txt

# Rebase a71eba2..e1a7dfa onto a71eba2
#
# 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
# # 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 -----------------------------------ps:--------------------------- ps:第一列是rebase具體執行的操作,其中操作可以選擇,其中含義如下: - 選擇pick操作,git會應用這個補丁,以同樣的提交資訊(commit message)儲存提交 - 選擇reword操作,git會應用這個補丁,但需要重新編輯提交資訊 - 選擇edit操作,git會應用這個補丁,但會因為amending而終止 - 選擇squash操作,git會應用這個補丁,但會與之前的提交合並 - 選擇fixup操作,git會應用這個補丁,但會丟掉提交日誌 - 選擇exec操作,git會在shell中執行這個命令

三、將第二個pick改成squash或者s ,然後儲存退出。(在Vim中是按下後“ESC”後輸入:wq,最後是按下回車),最後會出現下面的介面:(如果需要修改下提交資訊,如果不需要直接儲存退出即可。)

# This is a combination of 2 commits.
# The first commit's message is:

create second.txt

# This is the 2nd commit message:

add text in second.txt

# 請為您的變更輸入提交說明。以 '#' 開始的行將被忽略,而一個空的提交
# 說明將會終止提交。
#
# 日期:  Mon Nov 28 13:59:43 2016 +0800
#
# 變基操作正在進行中;至 a71eba2
# 您在執行將分支 'master' 變基到 'a71eba2' 的操作時編輯提交。
#
# 要提交的變更:
#   新檔案:   second.txt
#

四、修改成功