> For the complete documentation index, see [llms.txt](https://alanmpan.gitbook.io/git-learning/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://alanmpan.gitbook.io/git-learning/git/24.-bian-ji-he-he-bing.md).

# 24.变基和合并

`Rebasing`（变基） 和 `Merging`（合并） 是 Git 中常用的两种集成分支的方法。这两种方法都可以将一个分支的更改合并到另一个分支中，但它们的内部实现不同，因此其使用场景和结果也略有不同。

> rebase *v.* 重定（税收、价格指数等）的基准，变基的意思大概是改变基础吧。这个词念起来来别扭的很。

## Rebasing

`Rebasing` 是一种将分支更改应用于目标分支的方法，它会将分支的每个提交都转移到目标分支的顶部，并在每个提交之间将目标分支的更改应用于分支更改。这意味着，当你使用 `Rebasing` 方法时，最终的提交历史记录是一个线性的历史记录，其中所有更改都按照时间顺序排列。

主要优点：

* 提交历史记录更加干净和有序。
* 可以快速解决由于分支变化而导致的代码冲突。

主要缺点：

* 可能需要耗费大量时间和精力来处理冲突。
* 对于多人协作开发而言，可能需要进行协调才能确保不出现问题。

## Merging

`Merging` 是一种将分支更改合并到目标分支的方法。它会创建一个新的合并提交，该提交包含了目标分支和要合并的分支的全部更改。这意味着，当你使用 `Merging` 方法时，最终的提交历史记录将包含合并提交以及两个分支的更改历史记录。

主要优点：

* 容易理解和使用。
* 不需要手动处理冲突。

主要缺点：

* 提交历史记录可能会变得杂乱无序，难以阅读和理解。
* 如果分支更改频繁，可能会导致大量的冲突。

综上所述，`Rebasing` 和 `Merging` 都是有效的集成分支的方法，它们适用于不同的场景。如果你需要保持提交历史记录的整洁和有序，或者需要快速解决由于分支变化而导致的代码冲突，则可以选择使用 `Rebasing` 方法。如果你需要简单地将一个分支的更改合并到另一个分支中，并且不关心最终的提交历史记录，则可以选择使用 `Merging` 方法。


---

# 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://alanmpan.gitbook.io/git-learning/git/24.-bian-ji-he-he-bing.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.
