Skip to content

Rebase

一句话解释

Rebasing replays 你的分支's 提交 on 顶部的 another 分支, 创建 linear history 不使用合并提交.

用大白话说

git rebase takes the commits from your current branch and replays them one by one on top of another branch (e.g. main). The result is a straight line of commits — as if you had started your work from the latest main.

Rebase produces cleaner, linear history because it does not create merge commits. However, it rewrites commit hashes, which means anyone who has already pulled your branch will have divergent histories. The golden rule: never rebase branches that others have already pulled.

Use rebase for local feature branches to keep them up to date with main. Use merge for shared branches to preserve history and avoid rewriting commits others depend on. If a rebase hits conflicts, resolve them and run git rebase --continue, or git rebase --abort to cancel.

示例

bash
# Rebase feature onto latest main
git checkout feature
git rebase main

# Interactive rebase to squash commits
git rebase -i main

Rebase replays feature 提交 on 顶部的 主要 for linear history.

常见混淆

  • 容易混淆的对象: 合并

    关键区别: 合并组合分支 and 创建合并提交, preserving 分支 history. Rebase replays 提交 on 顶部的 目标, producing linear history 但 rewriting hashes. 合并是 safe for shared 分支; rebase 是 for 局部 cleanup.

相关术语

相关资源

相关课程

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误

← 返回术语表