git rebase <branchname>

Replay your current branch’s commits on top of another branch to create a cleaner, linear history.

This command is useful when you want to update your current branch with the latest changes from another branch while keeping a tidy, linear commit history instead of a series of merge commits.

It works by taking the commits that exist on your current branch but not on <branchname>, temporarily removing them, moving your branch pointer to <branchname>, and then replaying those commits one by one on top of that branch. The <branchname> parameter is the branch you want to rebase onto (for example main or develop). You can add --interactive (or -i) to edit, reorder, squash, or drop commits during the process, and --onto <new-base> to rebase a subset of commits onto a completely different base. The non-interactive form without extra flags simply replays your commits in order, stopping only if there are conflicts for you to resolve.

Using variations like git rebase -i <branchname> lets you rewrite your commit history as you rebase, which is helpful for cleaning up work before sharing it, while git rebase --continue, git rebase --abort, and git rebase --skip control how you proceed if conflicts occur. Closely related commands include git pull --rebase (which fetches and then rebases instead of merging) and git merge <branchname> (which combines histories with a merge commit rather than rewriting commits). For navigating between branches you rebase onto or from, git checkout <branchname> or git switch <branchname> are often used just before or after this command.

Examples:

  • git rebase main
  • git rebase develop
Manual page
git rebase
Related commands

Welcome to GitExamples!

Sign in to gitexamples