1. 程式人生 > >聊下git merge --squash

聊下git merge --squash

你經常會面臨著將dev分支或者很多零散的分支merge到一個公共release分支裡。

但是有一種情況是需要你處理的,就是在你的dev的分支裡有很多commit記錄。而這些commit是無需在release裡體現的。

develop 主分支

1

develop主分支最近的一個commit是”fix imageprint bug.”。我們拉出一個分支進行專案開發,裡面會有很多commit記錄。

git checkout -b develop_newfeature_ImportDataInterface origin/develop

develop_newfeature_ImportDataInterface 分支的commit log是和develop是一模一樣的。我們新增點修改commit。

2

現在我們需要將develop_newfeature_ImportDataInterface 分支的commit merge到develop主分支裡去。在合併進develop的時候我們希望在develop裡的commit log裡只看見一次commit,而且這個commit的message不是很隨意的,而是符合正規提交流程的約定格式的,比如,”develop:finished import data interface”。

我們就需要藉助git merge –squash命令引數來重新準備提交。(這是一個明確的merge操作不同於git rebase,不要搞混淆這兩個命令的作用。)

git merge --squash develop_newfeature_ImportDataInterface

Updating cc1fea6..e6fb522
Fast-forward
Squash commit -- not updating HEAD
1.txt | 3 +++
1 file changed, 3 insertions(+)

--squash 會暫停commit提交。要不然一個merge會自動提交commit。

3

1.txt檔案是我們修改的檔案,它現在待commit。現在我們只需要重新提交即可。

git commit -m'develop:finished import data interface'

4

這樣每次merge就會很清爽,一目瞭然,就算回頭reset也方便。

作者:王清培

本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段宣告,且在文章頁面