Skip to content

git rebase

Replaying 提交使用 git rebase — keeping history linear and resolving rebase conflicts.

你将学到

  • 如何 rebase replays 你的提交 on 顶部的 another 分支
  • 为什么 rebase 创建 cleaner, linear history than 合并
  • Interactive rebase for squashing, reordering, and 编辑提交
  • golden 规则: 从不 rebase 提交你 有 pushed

概念讲解

What git rebase Does

git rebase takes your commits and replays them on top of another branch. This rewrites history to make it look like your branch was created from the latest commit of the target branch, resulting in a clean, linear history.

git checkout feature-login
git rebase main

This replays your feature branch commits on top of the latest 主要.

Merge vs Rebase

With merge:           With rebase:
A---B---C---M  (main)     A---B---C---D'---E'  (main)
         \                          ^
          D---E  (feature)          |
                              (feature rebased)
  • Merge preserves both histories with a merge commit (M). The history shows the branch divergence.
  • Rebase replays your commits (D→D', E→E') on top of main. The history is linear — no merge commit.

Rebase produces cleaner history but rewrites commit hashes. The commits are not the same — they are new commits with the same changes.

When to Rebase

Rebase is ideal for:

  • Updating a feature branch with the latest main before merging
  • Cleaning up your commit history before a PR (squash, reorder, reword)
  • Keeping a linear history in your project's main branch
# Before merging a feature, rebase it on the latest main
git fetch origin
git checkout feature-login
git rebase origin/main
# resolve any conflicts, then:
git push --force-with-lease origin feature-login
# Now merge into main (will be a fast-forward, no merge commit)

Interactive Rebase

git rebase -i lets you rewrite history commit by commit:

git rebase -i HEAD~3

This opens an editor listing the last 3 commits:

pick abc1234 Add login form
pick def5678 Add validation
pick ghi9012 Fix typo in error message

You can change 选取 to:

  • 选取 — keep the commit as is
  • reword — keep the commit but change its message
  • squash — combine this commit with the previous one
  • fixup — like squash but discard this commit's message
  • drop — remove the commit entirely
  • 编辑 — pause to modify the commit's content

Squashing Commits

pick abc1234 Add login form
squash def5678 Add validation
squash ghi9012 Fix typo in error message

This combines all three into a single commit. You then edit the combined commit message.

Resolving Rebase Conflicts

If a commit cannot be replayed cleanly, rebase pauses:

git rebase origin/main
# CONFLICT (content): Merge conflict in login.js

# Resolve the conflict, then:
git add login.js
git rebase --continue

# To skip this commit:
git rebase --skip

# To cancel the rebase entirely:
git rebase --abort

Rebase conflicts are resolved one commit at a time, which can be more work than a single merge conflict. --abort is your escape hatch.

The Golden Rule of Rebase

Never rebase commits that you have already pushed to a shared branch.

Rebase rewrites commit hashes. If someone else has pulled your original commits, their history will diverge from your rebased history, creating a mess.

  • ✅ Rebase your local feature branch before pushing
  • ✅ Rebase a branch that only you use
  • ❌ Rebase 主要 or any shared branch
  • ❌ Rebase commits that others may have pulled

If you rebase a pushed branch, you must force push (git 推送 --force-使用-lease), which can overwrite others' work.

Rebase vs Merge: Guidelines

  • Feature branches: rebase onto main before merging (clean history)
  • Main branch: never rebase (others depend on it)
  • Long-lived branches: merge (rebase would be too disruptive)
  • Small teams: rebase for linear history
  • Large teams: merge to preserve the branch history and avoid force-push issues

示例代码

              # --- Basic rebase: update feature branch with latest main ---
git fetch origin
git checkout feature-login
git rebase origin/main
# if conflicts occur, resolve them:
git add conflicted-file.js
git rebase --continue
# push the rebased branch (force needed because history changed)
git push --force-with-lease origin feature-login

# --- Interactive rebase: squash last 3 commits ---
git rebase -i HEAD~3
# In the editor, change to:
# pick abc1234 Add login form
# squash def5678 Add validation
# squash ghi9012 Fix typo
# Save and edit the combined commit message

# --- Interactive rebase: reword a commit ---
git rebase -i HEAD~3
# change 'pick' to 'reword' for the commit you want to rename
# save, then edit the commit message

# --- Interactive rebase: drop a commit ---
git rebase -i HEAD~5
# change 'pick' to 'drop' for commits to remove
# or just delete the line

# --- Rebase to reorder commits ---
git rebase -i HEAD~3
# Just reorder the lines in the editor

# --- Abort a rebase that is going wrong ---
git rebase --abort

# --- Skip a commit that cannot be applied ---
git rebase --skip

# --- Pull with rebase (combine fetch + rebase) ---
git pull --rebase origin main

# --- Configure pull to always rebase ---
git config --global pull.rebase true
            

Shows 基础 rebase, interactive rebase for squashing/rewording/dropping/reordering 提交, conflict resolution, aborting, skipping, and 拉取 --rebase. --force-使用-lease flag 是必要之后 rebasing pushed 分支. golden 规则: 从不 rebase shared 提交.

动手试一试

  • Code Diff

    Review 更改之前 rebasing 到 anticipate conflicts.

  • CI/CD YAML Generator

    Rebased 分支触发 CI — generate 流水线到 验证.

常见错误

错误写法

Rebasing 提交那个是 already pushed and others 有 pulled

正确做法

golden 规则: 从不 rebase shared 提交. Rebase rewrites 提交 hashes, 所以 anyone 谁 pulled 原始提交将 有 divergent history. 仅 rebase 局部, unpushed 提交 or 你的 own feature 分支那个否 one else uses. 如果你 必须 rebase pushed 分支, 坐标使用你的 team and use --force-使用-lease.

错误写法

Using git 推送 --force 相反的 --force-使用-lease 之后 rebase

正确做法

git 推送 --force blindly overwrites remote, potentially destroying teammates' 提交. git 推送 --force-使用-lease 仅 force pushes 如果 remote 有不 更改由于你 最后获取. 总是 use --force-使用-lease 之后 rebasing — 它是 safe alternative 到 --force.

相关速查表

练习 git rebase

5 个练习

练习 git rebase

查阅不认识的术语

新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。

查看术语表

动手做项目

通过引导式项目应用所学,含起始代码与解决方案。

查看项目

解读错误信息

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

查看错误