Git rebase

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.



Tutorials dojo strip

Initial Example

To rebase your current branch onto another base branch, use:

$ git rebase <base>




REBASE Options

OptionDescription
-i, --interactiveRebase in interactive mode. Allows you to edit, reorder, or squash commits.
-p, --preserve-mergesRecreate 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.
--ontoRebase onto a specific commit instead of the direct parent.
--keep-baseKeep the base commit of the branch being rebased.
--continueContinue the rebase process after resolving conflicts.
--abortAbort the rebase and reset to the original branch.
--skipSkip the current commit and continue with the rebase.
-X <strategy-option>Pass a specific option to the merge strategy.
--autostashAutomatically stash local changes before rebasing and apply them after the rebase.
--no-autostashDo 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
Scroll to Top