Git 2 reference · Branching and Merging

git rebase Command (Git)

Replay your branch's commits on top of another branch for a linear history, or clean up commits with -i before sharing.

Git 2

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

git rebase [BRANCH]   git rebase -i COMMIT   git rebase --continue|--abort

Parameters

ParameterDescription
BRANCH Replay current commits onto BRANCH.
-i COMMIT Interactively reorder, squash, reword.
--continue / --abort After resolving conflicts / give up.
--autosquash Apply fixup! commits.

Examples

Rebase a feature branch onto main
git init -q -b main app && cd app
echo '# Shop' > README.md
git add README.md && git commit -q -m 'Initial commit'
git switch -q -c feature; echo f > f.txt; git add .; git commit -q -m 'Feature work'
git switch -q main; echo m > m.txt; git add .; git commit -q -m 'Main work'
git switch -q feature; git rebase -q main
git log --oneline --graph

Output

* 7f3521d Feature work
* 1def0b6 Main work
* 7f7f133 Initial commit

Run this on your computer — see “Get Started” for setup.

Squash a fixup commit
git init -q -b main app && cd app
echo '# Shop' > README.md
git add README.md && git commit -q -m 'Initial commit'
echo 1 > a.txt; git add .; git commit -q -m 'Add a'
echo 2 >> a.txt; git commit -q -am 'fixup! Add a'
GIT_SEQUENCE_EDITOR=true git rebase -q -i --autosquash HEAD~2
git log --oneline

Output

baa3b86 Add a
7f7f133 Initial commit

Run this on your computer — see “Get Started” for setup.

Never rebase commits that others already have; rebasing rewrites them. Rebase your own unpushed or feature-branch work.

Was this page helpful?