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
Syntax
git rebase [BRANCH] git rebase -i COMMIT git rebase --continue|--abort
Parameters
| Parameter | Description |
|---|---|
BRANCH |
Replay current commits onto BRANCH. |
-i COMMIT |
Interactively reorder, squash, reword. |
--continue / --abort |
After resolving conflicts / give up. |
--autosquash |
Apply fixup! commits. |
Examples
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.
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.
Related
git merge· Combine another branch into the current one, fast-forwarding when possible or creating a merge commit.git cherry-pick· Apply the changes of specific commits from another branch onto the current branch as new commits.- Rebasing · tutorial