git rebase
integrates two branches into a single branch, similar to git merge
, but with one key difference: it rewrites the commit history. This makes the commit history linear, which can be useful for consolidating multiple private branches into one.
Initial Example
To rebase your current branch onto another base branch, use:
$ git rebase <base>
REBASE Options
Option | Description |
---|---|
-i , --interactive | Rebase in interactive mode. Allows you to edit, reorder, or squash commits. |
-p , --preserve-merges | Recreate merge commits instead of flattening the history. |
-r , --rebase-merges[=<rebase-merges-options>] | Rebase merge commits by flattening them, or use the options to control the behavior of the merge commits during rebase. |
--onto | Rebase onto a specific commit instead of the direct parent. |
--keep-base | Keep the base commit of the branch being rebased. |
--continue | Continue the rebase process after resolving conflicts. |
--abort | Abort the rebase and reset to the original branch. |
--skip | Skip the current commit and continue with the rebase. |
-X <strategy-option> | Pass a specific option to the merge strategy. |
--autostash | Automatically stash local changes before rebasing and apply them after the rebase. |
--no-autostash | Do not automatically stash local changes before rebasing. |
Examples
1. Basic Rebase
To rebase your current branch onto the main
branch:
- First, ensure you are on the branch you want to rebase:
$ git checkout feature
- Then, run the rebase command:
$ git rebase main
2. Resolving Conflicts
If there are conflicts during the rebase, Git will pause and highlight the conflicts:
- Check the conflicted files:
$ git status
- Resolve the conflicts by editing the files:
$ nano conflicted_file.txt
- After resolving the conflicts, add the files to the staging area:
$ git add conflicted_file.txt
- Continue the rebase process:
$ git rebase --continue