linuxlab.io
Tutorials▾
  • Linux & networking
    File system, processes, TCP/IP, BGP and OSPF
    →
  • Terraform & IaC
    HCL, state, plan/apply on a LocalStack sandbox
    →
  • Git & GitHub
    Object model, plumbing, branching, GitHub Actions
    →
All tutorials →
PricingAboutSign inCreate account
/
Intro
Lessons
Footer
linuxlab-TutorialsPricingAboutPrivacy & cookies
Copyright © 2026 LinuxLab. All rights reserved.
linuxlab.io
Tutorials▾
  • Linux & networking
    File system, processes, TCP/IP, BGP and OSPF
    →
  • Terraform & IaC
    HCL, state, plan/apply on a LocalStack sandbox
    →
  • Git & GitHub
    Object model, plumbing, branching, GitHub Actions
    →
All tutorials →
PricingAboutSign inCreate account
/
  • Introduction
  • Chapters
  • How it works
  • Lessons
  • Knowledge base
  • Interview prep
home/git/kb/Branches & merging/rebase

kb/branching ── Branches & merging ── intermediate

git rebase

Rewrites the commits of a branch so they descend from a different commit. Each commit gets a new SHA; history becomes linear. Safe only on branches that no one else has seen.

view as markdownaka: git-rebase

git rebase <base> takes the commits of the current branch from its common ancestor with <base> up to HEAD, and replants them on top of <base>. Each commit gets a new parent, so each commit gets a new SHA.

Basic scenario

before:  main:  A → B → E
                   \
         feat:      C → D
git switch feat
git rebase main
after:   main:  A → B → E
                        \
         feat:              C' → D'    ← new SHAs

The changes in C' and D' are the same as in C and D. But they are different objects: for Git they are new commits.

When to use it

  • Before a PR, so the feature branch sits on top of a fresh main with no merge commits.
  • Cleaning up local history via rebase -i (see interactive-rebase).
  • Moving a branch to a different base with --onto.

The main rule

Do not rebase public branches. If someone has cloned the branch, they still have the old SHAs. After your force-push their history diverges. Rebase is safe only on your personal feature branch, before or immediately after review.

Conflicts

If a conflict occurs on any commit, Git stops, marks the conflicting files, and waits for you to resolve them:

bash
# edit the files, remove the markers
git add <files>
git rebase --continue      # continue with the next commit
# or skip the current commit:
git rebase --skip
# or cancel everything:
git rebase --abort

A conflict can appear on every commit in a row if each one touches the disputed lines. After rebase each intermediate result is already clean.

After rebase

If the branch was already pushed, a plain push will be rejected because the SHAs changed:

bash
git push --force-with-lease

--force-with-lease verifies that the remote has not moved forward (no one else pushed in the meantime). Always prefer --force-with-lease over --force.

Pitfalls

  • Rebase copies author dates as-is but updates committer dates. After a rebase, git log --pretty=fuller may show seemingly odd discrepancies between the two.
  • On a large number of commits, rebase can take noticeable time and may require resolving conflicts at each step.
  • git pull --rebase combines fetch with a rebase of your local commits on top of the remote ones. A useful default: git config --global pull.rebase true.

§ команды

bash
git rebase main

Replant the current branch on top of main

bash
git rebase -i HEAD~5

Interactive rebase: rename/squash/delete the last 5 commits

bash
git rebase --continue

Continue rebase after resolving a conflict

bash
git rebase --abort

Cancel the rebase in progress and return to the original state

§ см. также

  • mergegit mergeMerges another branch into the current one. Either fast-forwards the pointer or creates a merge commit with two parents. Overlapping changes produce conflicts.
  • interactive-rebaseInteractive rebase`git rebase -i <base>` opens an editor with the list of commits from `<base>` to HEAD, where you can rename, combine, delete, and reorder them. It is the primary tool for cleaning up local history.
  • cherry-pickgit cherry-pickTakes one commit from another branch and applies it to the current one, creating a duplicate with a new SHA. Used for hotfixes across multiple release branches and for moving individual commits.
Footer
linuxlab-
Copyright © 2026 LinuxLab. All rights reserved.
Tutorials
Pricing
About
Privacy & cookies