> For the complete documentation index, see [llms.txt](https://jen-hsuan-hsieh.gitbook.io/debugging-tools-experience/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jen-hsuan-hsieh.gitbook.io/debugging-tools-experience/chapter4gong-ju/chapter1/213rebase.md).

# 4.1.4.rebase

## 1.Introduction

* 由於branch與master分支的時間點不同, rebase是將branch的commit由舊的分支點移向新的分支點, 藉以讓master上上新的patch
* 與merge不同的是: merge是直接在master上直接加一個commit, 而rebase是在master上面加上新的commit
* 兩者的commit graph也不同
  * merge

    ![](https://github.com/jenhsuan/debugging-tools-experience/tree/91ad7a157f155ea77065c5600800bb4627bd1ae2/assets/螢幕快照%202017-09-17%20上午10.44.34.png)
  * rebase

    ![](https://github.com/jenhsuan/debugging-tools-experience/tree/91ad7a157f155ea77065c5600800bb4627bd1ae2/assets/螢幕快照%202017-09-17%20上午10.46.41.png)

## 2.Getting started

* 1.git log
  * 取得當前分支上的所有commit

    ![](/files/-M4M0NdtyI8BfNLjHXlL)
* 2.git rebase -i HEAD\~\~
  * 將最新的以及次新的commit合併

    ![](/files/-M4M0NdvFSQ_RPT0kKu9)
  * 將第二行的pick改為squash

    ![](/files/-M4M0Ndx8-v-HeeH1DIw)
  * 編輯新的commit message

    ![](/files/-M4M0NdzlxXlidSWlb20)
* 3.git log
  * 合併成功

    ![](/files/-M4M0Ne0bVTP6PNlRz43)

## 3.rebase的優缺點

* 可讀性佳: rebase的resulting history是線性的, 而且不會有divergent branches
* 導致錯誤
  * 當branch上所依賴的部分在master上被刪除了, 但是由於在rebase時並不會顯示出來, 因此rebase的執行並不會被中斷而導致一連串錯誤的commit, 這時候必須再加上一個新的commit來補救&#x20;
  * 所以該如何避免在rebase時一連串錯誤的commit呢?
    * 1.在rebase完後測試code以確認是否有bug
    * 2.用Git merge取代之, 用一個commit處理所有的conflict, 可以history也可以描述何時合併, 合併時修改了哪些東西


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://jen-hsuan-hsieh.gitbook.io/debugging-tools-experience/chapter4gong-ju/chapter1/213rebase.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
