教學1 使用分支

7. 使用 rebase 合併

另外,您也可以使用rebase命令將 issue3 分支合併到master分支,這樣的話歷史記錄會顯得更簡單,像之前描述的一樣。

現在,我們先暫時取消剛才的合併。

$ git reset --hard HEAD~

rebase前的歷史記錄

切換到 issue3 分支後,對 master 執行 rebase 。

$ git checkout issue3
Switched to branch 'issue3'
$ git rebase master
First, rewinding head to replay your work on top of it...
Applying: 添加pull的說明
Using index info to reconstruct a base tree...
<stdin>:13: new blank line at EOF.
+
warning: 1 line adds whitespace errors.
Falling back to patching base and 3-way merge...
Auto-merging myfile.txt
CONFLICT (content): Merge conflict in myfile.txt
Failed to merge in the changes.
Patch failed at 0001 添加pull的說明

When you have resolved this problem run "git rebase --continue".
If you would prefer to skip this patch, instead run "git rebase --skip".
To check out the original branch and stop rebasing run "git rebase --abort".

和 merge 時的操作相同,修改在 myfile.txt 發生衝突的地方。

連猴子都懂的Git命令
add 修改加入索引
<<<<<<< HEAD
commit 記錄索引的狀態
=======
pull 取得遠端數據庫的內容
>>>>>>> issue3

修改好衝突後,若想要繼續 rebase 的操作,請在rebase後加上 --continue。若要取消 rebase 的話,請使用 --abort。

$ git add myfile.txt
$ git rebase --continue
Applying: 添加pull的說明

目前的歷史記錄

這樣,在 master 分支的 issue3 分支就可以 fast-forward 合併了。切換到 master 分支後執行合併。

$ git checkout master
Switched to branch 'master'
$ git merge issue3
Updating 8f7aa27..96a0ff0
Fast-forward
 myfile.txt |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)

myfile.txt 的最終內容和 merge 是一樣的,但是歷史記錄如下:

目前的歷史記錄