git 合并
组合分支使用 git 合并 — 快-向前合并, three-方式合并, and 合并提交.
你将学到
- 如何 合并 feature 分支到 主要
- difference 之间快-向前 and three-方式合并
- 什么合并提交是 and 当它 是创建
- 如何 abort 合并那个 goes 错误
概念讲解
What git merge Does
git 合并 combines the changes from one branch into another. This is how you integrate a completed feature back into the main branch.
git checkout main
git merge feature-login
You always switch to the branch you want to merge into (the target), then merge the branch you want to merge from (the source).
Fast-Forward Merges
If the target branch has not changed since the source branch was created, Git simply moves the target pointer forward. This is a fast-forward merge — no new commit is created.
Before: A---B---C (main)
\
D---E (feature)
After: A---B---C---D---E (main, feature)
Fast-forward merges keep the history linear and clean. Git does this automatically when possible.
Three-Way Merges
If both branches have new commits, Git must create a merge commit that combines both lines of development. This is a three-way merge — it uses the two branch tips and their common ancestor.
Before: A---B---C (main)
\
D---E (feature)
\
F---G (main moved on)
After: A---B---C---F---G---M (main)
\ /
D---E--- (feature)
M is the merge commit with two parents. This preserves the history of both branches.
Forcing a Merge Commit
Even when a fast-forward is possible, you might want a merge commit to record that a feature was merged:
git merge --no-ff feature-login
--否-ff (no fast-forward) always creates a merge commit. This is useful for feature branch workflows where you want a record of each merged feature.
Resolving Conflicts
If both branches changed the same lines, Git cannot merge automatically and reports a conflict. You must resolve it manually:
git merge feature-login
# CONFLICT (content): Merge conflict in login.js
# Edit the file to resolve conflict markers
# Then:
git add login.js
git commit
Conflict resolution is covered in detail in the merge-conflicts lesson.
Aborting a Merge
If a merge goes wrong or you are not ready to resolve conflicts:
git merge --abort
This cancels the merge and returns your working directory to its pre-merge state. Use this whenever you feel overwhelmed by a conflict — you can always try again.
Merge Strategies
- Merge commit (default) — preserves both histories with a merge commit
- Rebase — replays your commits on top of the target, keeping linear history
- Squash — combines all feature commits into a single commit on the target
Each has trade-offs. The merge-conflicts and git-rebase lessons cover these in depth.
示例代码
# Create and work on a feature branch
git checkout -b feature-user-profile
echo "profile logic" > profile.js
git add profile.js
git commit -m "feat: add user profile page"
# Switch to main and merge the feature
git checkout main
git merge feature-user-profile
# If main also moved forward, you get a three-way merge
# Let's simulate that:
git checkout -b another-feature
echo "more" > more.js
git commit -am "feat: add more"
git checkout main
echo "main change" >> README.md
git commit -am "docs: update README"
git merge another-feature # creates a merge commit
# Force a merge commit even when fast-forward is possible
git merge --no-ff feature-user-profile
# If a conflict occurs, abort and try again later
git merge conflicting-branch
git merge --abort
# After resolving a conflict manually:
git add resolved-file.js
git commit # completes the merge
# Delete the merged branch (safe now)
git branch -d feature-user-profile
Shows 基础合并, three-方式合并当 两者都分支 moved, forcing 合并提交使用 --否-ff, aborting problematic 合并, completing 合并之后 resolving conflicts, and cleaning 上合并分支. 合并 --abort command 是你的 safety net.
动手试一试
- Code Diff
Compare 分支之前合并到 preview 什么将 更改.
- CI/CD YAML Generator
合并到 主要经常触发部署流水线.
常见错误
错误写法
合并 in 错误方向 — 合并主要到 feature 分支
正确做法
总是切换到 目标分支 (e.g., 主要) 首先, 然后合并源 分支 (e.g., feature). command 是: git checkout 主要 && git 合并 feature. 合并主要到 feature 更新 feature 分支, 哪个是 通常不 什么你 想要.
错误写法
Panicking 当合并 conflict appears and 关闭 terminal
正确做法
Conflicts 是 normal. 如果你 是不 ready 到 resolve 它们, 运行 git 合并 --abort 到取消 and 返回到 pre-合并状态. Take 你的时间, 读取 conflict markers, resolve 它们 in 你的 editor, 然后 git 添加 and git 提交.
相关资源
相关速查表
- Git Cheatsheet
合并, 合并 --否-ff, and 合并 --abort commands.
练习 git 合并
10 个练习
查阅不认识的术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
动手做项目
通过引导式项目应用所学,含起始代码与解决方案。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。