git合併歷史提交
背景
以前一直覺得只要pull和push就夠了,但合作中總會遇到各種非理想的情況。這時候才發現git其他命令的作用。
現在的情況是,repo是一個遠端team維護的,我們需要增加新feature,那麼就是一個feature分支了。由於開發中各種修改,本feature分支多次commit。最後,交給遠端team review的時候,人家看著亂七八糟的修改歷史很蛋疼:很難看懂各種增量修改。其實,對人家來說,我們的改動應該就是增加或者刪除。給他們看開發過程的增量反而太亂。於是,人家要求我們將feature分支的提交合並,這樣看起來清爽。
一些簡單的命令準備
合併分支的命令是rebase
,除此之外,其他的一些命令也應該知曉。
檢視commit歷史
git log
檢視當前狀態
git status
新增所有檔案
git add .
提交修改
git commit -m "本次提交添加了xxxx"
vim的簡單指令:
參閱vim的簡單使用
準備一個測試repo
git init test-rebase
cd test-rebase
提交一個檔案多次:
vim test.txt //輸入第一次提交。 git add test.txt git commit -m "1" vim test.txt //輸入第2次提交。 git add test.txt git commit -m "2" vim test.txt //輸入第3次提交。 git add test.txt git commit -m "3"
檢視log:
git log ////// commit 0353373749d72e53a34c7bdda86d77d7bb3ca6fe Author: ryan <[email protected]> Date: Wed Jul 19 13:23:18 2017 +0800 3 commit acf6d24adc2097fda82d29064e8edfef6355d01d Author: ryan <[email protected]> Date: Wed Jul 19 13:20:37 2017 +0800 2 commit 2169bc5e20386951b19aff32143e74f2da683df2 Author: ryan <[email protected]> Date: Wed Jul 19 13:19:42 2017 +0800 1
可以看到有三次提交了。現在我們想要把第2次和第3次提交的內容合併成一次提交。
開始rebase
1. 複製合併前的一次提交的hash
這裡就是第一次提交的hash。即2169bc5e2
2. git rebase -i xxx
git rebase -i 2169bc5e2
進入歷史提交的編輯頁面,此時編輯方式為vim。
pick acf6d24 2
pick 0353373 3
# Rebase 2169bc5..0353373 onto 2169bc5 (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
可以看到第2次和第3次的提交訊息,並且是從old->new來排序的。我們需要把第3次提交合併到第2次上。使用squash
.
squash
修改第三次提交為squash
,意思是和前一次(第二次)提交合並。
鍵盤按鍵j
移動到第二行,然後按a
開始編輯,刪除pick
,插入squash
如下:
pick acf6d24 2
squash 0353373 3
# Rebase 2169bc5..0353373 onto 2169bc5 (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
然後,按esc
退出編輯,再按:
,輸入wq
儲存。
這時候會進入第二個vim頁面,這裡讓我們再次修改commit message的。就是合併後的message。
# This is a combination of 2 commits.
這是合併後的message,以下是之前合併的歷史
# This is the 1st commit message:
2
# This is the commit message #2:
3
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date: Wed Jul 19 13:20:37 2017 +0800
#
# interactive rebase in progress; onto 2169bc5
# Last commands done (2 commands done):
# pick acf6d24 2
# squash 0353373 3
# No commands remaining.
# You are currently editing a commit while rebasing branch 'master' on '2169bc5'.
#
# Changes to be committed:
還是和剛才一樣,按o
插入下一行,輸入這次合併的message。然後按esc
,按:
, 輸入wq
儲存並退出。
完事,再次檢視log
git log
內容如下:
commit 8f54e6b5643ff26ac967a9e6e6cded68a6c50906
Author: ryan <[email protected]>
Date: Wed Jul 19 13:20:37 2017 +0800
這是合併後的message,以下是之前合併的歷史
2
3
commit 2169bc5e20386951b19aff32143e74f2da683df2
Author: ryan <[email protected]>
Date: Wed Jul 19 13:19:42 2017 +0800
1
參考
https://git-scm.com/book/zh/v2/Git-工具-重寫歷史
https://www.cnblogs.com/wt645631686/p/13550192.html