git rebase

Rebasing moves your branch's commits so they start from the tip of another branch, producing a cleaner, linear history. It is an alternative to merging that avoids extra merge commits.

Git & DevOps

Where a merge ties two histories together with a merge commit, a rebase replays your commits one by one on top of the latest target branch, as if you had written them there from the start.

The golden rule of rebasing: don't rebase commits that other people have already pulled, because it rewrites history and can cause confusion. Rebasing is best for tidying up your own local branch before merging.

Syntax

git rebase <branch>

Examples

Bash

git switch feature-login
git rebase main

Output:

Successfully rebased and updated refs/heads/feature-login.

Learn it in a lesson

See also

Back to the Dictionary