Sobes.tech
Junior

What does it mean to rebase onto the main branch?

sobes.tech AI

Answer from AI

Performing git rebase main from the current branch means moving the branch's starting point to the latest commit of main.

This happens as follows:

  1. Git temporarily saves the commits from your current branch that appeared after its divergence point from main.
  2. Your current branch is reset to the state of the main branch.
  3. The saved commits are then sequentially applied on top of the latest commit of main.

Main differences from merge:

Characteristic Rebase Merge
History Linear, no merge commits Non-linear, includes merge commits
History clarity Cleaner, easier to understand Can be more complex to understand
Conflicts Resolved for each commit separately Resolved once for all changes
Usage Preferable for small, personal branches Preferable for integrating public branches

Example:

Initial history:

A -- B -- C (main)
      \
       D -- E (feature)

After git checkout feature and git rebase main:

A -- B -- C (main)
           \
            D' -- E' (feature)

Where D' and E' are commits D and E with a new parent starting point.

Possible issues:

  • Conflicts: May occur when applying each commit.
  • Rewriting history: rebase changes commit IDs. This is dangerous if you rebase a branch that has already been published and others are working on. In such cases, a force push (git push --force) may be required, which should be done cautiously.