LinuxCommandLibrary

git-rebase

Integrate changes from one branch onto another

TLDR

Rebase the current branch on top of another specified branch

$ git rebase [new_base_branch]
copy

Start an interactive rebase, which allows the commits to be reordered, omitted, combined, or modified
$ git rebase [[-i|--interactive]] [target_base_branch_or_commit_hash]
copy

Continue a rebase that was interrupted by a merge failure, after editing conflicting files
$ git rebase --continue
copy

Continue a rebase that was paused due to merge conflicts, by skipping the conflicted commit
$ git rebase --skip
copy

Abort a rebase in progress (e.g. if it is interrupted by a merge conflict)
$ git rebase --abort
copy

Move part of the current branch onto a new base, providing the old base to start from
$ git rebase --onto [new_base] [old_base]
copy

Reapply the last 5 commits in-place, stopping to allow them to be reordered, omitted, combined, or modified
$ git rebase [[-i|--interactive]] [HEAD~5]
copy

Auto-resolve any conflicts by favoring the working branch version (theirs keyword has reversed meaning in this case)
$ git rebase [[-X|--strategy-option]] theirs [branch_name]
copy

SYNOPSIS

git rebase [-i | --interactive] [options] [--onto newbase] [upstream] [branch]
git rebase --continue | --abort | --skip | --edit-todo | --quit

PARAMETERS

-i, --interactive
    Interactive mode: edit, squash, reorder commits via editor

--onto newbase
    Start rebase at newbase instead of upstream; rebases commits after upstream

--continue
    Resume rebase after resolving conflicts

--abort
    Abort rebase, restore original branch state

--skip
    Skip current commit, continue with next

-p, --preserve-merges
    Deprecated: preserve merge commits (use -r or --rebase-merges)

-r, --rebase-merges
    Recreate merge commits during rebase

-q, --quiet
    Be quiet; suppress progress output

--autosquash
    Automatically squash fixup! and squash! commits

--exec cmd
    Run cmd after each commit in interactive rebase

--root
    Rebase all commits reachable from current HEAD

-s, --strategy strategy
    Use merge strategy (e.g., recursive, ort)

DESCRIPTION

git rebase integrates changes from one branch into another by moving (replaying) commits to a new base, creating a linear history without merge commits. Unlike git merge, which preserves branch structure, rebase rewrites history, making it cleaner but riskier.

Basic usage: Switch to feature branch (git checkout feature), then git rebase main. Git replays feature commits atop latest main. If conflicts arise, resolve them, add files (git add), and continue (git rebase --continue).

Interactive rebase (-i) opens editor for each commit, allowing squash, edit, drop, reorder, or reword. Ideal for cleaning history before pushing.

Use cases: Update feature branch with upstream changes; integrate topic branches linearly. Avoid on shared/public branches to prevent rewriting others' history.

Advanced: --onto rebases subset of commits. Supports strategies like recursive or ort. Post-rebase, force-push with git push --force-with-lease if safe.

Word count approx. 220.

CAVEATS

Never rebase public/shared branches: rewrites history, breaks collaborators' clones.
Backup branch before rebasing.
Conflicts require manual resolution; use --abort to escape.
Force-push needed post-rebase (--force-with-lease safer than --force).
Not for binary files or large histories (slow, risky).

COMMON WORKFLOW

git checkout feature
git rebase main
Resolve conflicts, git add, git rebase --continue
git checkout main; git merge feature (fast-forward)

INTERACTIVE EXAMPLE

git rebase -i HEAD~3
Editor shows commits: change 'pick' to 'squash', 'edit', 'drop'; save & exit.
Use for cleaning commits before sharing.

HISTORY

Introduced in Git 1.0.1 (2005) by Junio C Hamano as shell script git-rebase.sh. Interactive mode added in Git 1.5.0 (2007). Rewritten in C (git-rebase--am) for speed. --rebase-merges and ort strategy in recent versions (2.25+, 2.34+). Evolved for safer history rewriting.

SEE ALSO

Copied to clipboard