> 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/30.-la-qu-bing-he-bing.md).

# 30.拉取并合并

`git pull` 可以一步实现 `git fetch`（拉取） 和`git merge`（合并） 的功能。

`git fetch` 命令会获取远程存储库中的所有代码更改，并将它们存储在名为 "remote branches" 的本地分支中。这些本地分支反映了远程存储库中存在的每个分支和其相应的提交历史记录。由于 `fetch` 不会自动将代码更改合并到当前活动分支中，因此您必须手动将其合并，例如使用 `git merge` 命令。

> remote branches 在本地看来是远程分支信息，实际上本地已经存储了

`git pull` 命令则会自动获取远程存储库的代码更改，并将其合并到当前活动分支中。具体来说，`pull` 命令首先运行 `git fetch`，然后自动将远程存储库的代码更改合并到当前活动分支中。

`git merge` 命令是一个用于手动合并两个分支的命令。通常，它用于将本地存储库中的更改合并到远程存储库中或将一个分支中的更改合并到另一个分支中。它需要两个参数，即要合并到当前分支的目标分支和要合并的源分支。

以下是这些命令之间关系的总结：

* `git fetch`: 获取远程存储库中的最新代码更改，并将其保存在本地存储库中的 "remote branches" 中。
* `git pull`: 运行 `git fetch` 命令以获取远程存储库中的最新代码更改，然后自动将其合并到当前活动分支中。
* `git merge`: 手动将两个分支合并，并创建一个新的合并提交记录。

需要注意的是，尽管 `pull` 命令可以更快地获取远程存储库的最新代码更改并将其合并到本地存储库中，但它也可能会导致合并冲突。因此，在运行 `pull` 命令之前，请确保所有本地更改都已提交或保存。


---

# 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/30.-la-qu-bing-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.
