> 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/21.-he-bing-fen-zhi.md).

# 21.合并分支

在 Git 中，合并分支是一种将两个不同的分支中的代码更改合并到一个分支中的操作。这允许团队成员在不同的分支中开发功能和修复错误，并最终将它们合并到主分支或其他稳定分支中。

以下是一些常见的 Git 合并命令：

* `git merge <branch-name>`: 将指定的分支合并到当前分支。
* `git merge --abort`: 取消正在进行的合并操作。
* `git merge --no-ff <branch-name>`: 强制使用新的提交记录来创建合并提交，而不是采用快进模式（Fast-forward），从而保留分支历史记录。

## 将 master 合并至 lgnew

```powershell
# 切换分支
$ git checkout lgnew

# 合并
$ git merge  master
Merge made by the 'ort' strategy.
lab/test3.txt | 0
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 lab/test3.txt
```

将 `lgnew` 分支中的所有更改合并到 `master` 分支中，并创建一个新的合并提交记录。

```powershell
# 查看提交日志
$ git hist --all
 
*   1ef2ec3 2023-05-05 | Merge branch 'master' into lgnew (HEAD -> lgnew) [aku]
|\
| * 1c78eab 2023-05-05 | Added teset3.txt (master) [aku]
* | 3d99c8f 2023-05-05 | Added teset2.txt [aku]
|/
* 5dc8b1e 2023-05-05 | Added .gitignore [aku]
* b77158a 2023-05-05 | Moved test.txt to lab [aku]
* 929f644 2023-05-05 | Added 123456 to the test.txt [aku]
| * 739560c 2023-05-05 | Oops,an error committed (tag: oops) [aku]
| * 1ca1854 2023-05-05 | Revert "Added 123 to the test.txt" [aku]
| * 375f4fe 2023-05-05 | Added 123 to the test.txt [aku]
|/
* d7f681f 2023-05-05 | Added abc to the test.txt (tag: v1) [aku]
* 01b8702 2023-05-05 | Add first file (tag: v1-beta) [aku]
```
