How to use the command git rebase (with examples)
Git rebase is a command used to reapply commits from one branch on top of another branch. It is commonly used to “move” an entire branch to another base, creating copies of the commits in the new location.
Use case 1: Rebase the current branch on top of another specified branch
Code:
git rebase new_base_branch
Motivation: This use case is useful when you want to update your current branch with the latest changes from another specified branch. By rebasing, you can incorporate the changes made in the new_base_branch into your branch’s history.
Explanation:
git rebase
: the command to initiate the rebase operation.new_base_branch
: the branch that you want to rebase onto your current branch.
Example output:
Successfully rebased and updated refs/heads/current_branch.
Use case 2: Start an interactive rebase
Code:
git rebase -i target_base_branch_or_commit_hash
Motivation: Sometimes, you might need to modify, reorder, or combine commits in your branch before merging them. The interactive rebase allows you to do exactly that by opening a text editor with a list of commits that can be reordered, omitted, combined, or modified.
Explanation:
git rebase -i
: initiates an interactive rebase.target_base_branch_or_commit_hash
: the target branch or commit hash you want to rebase onto.
Example output:
pick 895a4b2 Commit 1
pick 67ce5d8 Commit 2
...
Use case 3: Continue a rebase after editing conflicting files
Code:
git rebase --continue
Motivation: When a rebase encounters conflicts with the changes being applied, it pauses and waits for the conflicts to be resolved. After manually editing the conflicting files, you can continue the rebase using this command.
Explanation:
git rebase --continue
: resumes the rebase operation after resolving conflicts.
Example output:
Applying: Commit 1
Use case 4: Continue a rebase by skipping the conflicted commit
Code:
git rebase --skip
Motivation: In some cases, you might decide to skip a commit during the rebase process if it’s causing conflicts that you don’t want to resolve. This command allows you to continue the rebase while skipping the conflicted commit.
Explanation:
git rebase --skip
: skips the current conflicted commit and continues the rebase.
Example output:
Skipping commit: Commit 2
Use case 5: Abort a rebase in progress
Code:
git rebase --abort
Motivation: If a rebase encounters an unsolvable conflict or if you simply want to cancel the rebase operation, you can use this command to abort the rebase.
Explanation:
git rebase --abort
: cancels the ongoing rebase operation.
Example output:
Aborting
Use case 6: Move part of the current branch onto a new base
Code:
git rebase --onto new_base old_base
Motivation: This use case is helpful when you want to move a specific portion of your current branch onto a new base. It allows you to select a subrange of commits from the old_base to the current branch that will be reapplied on top of the new_base.
Explanation:
git rebase --onto
: specifies that you want to move some commits.new_base
: the branch or commit hash that you want to set as the new base.old_base
: the branch or commit hash that marks the starting point of the commits you want to move.
Example output:
Successfully moved commits from old_base to new_base.
Use case 7: Reapply the last 5 commits in-place with an interactive rebase
Code:
git rebase -i HEAD~5
Motivation: With this use case, you can interactively rebase and modify the last 5 commits on your branch. It allows you to reorder, omit, combine, or modify these commits before they are reapplied.
Explanation:
git rebase -i
: starts an interactive rebase operation.HEAD~5
: references the 5th commit before the current commit.
Example output:
pick 67ce5d8 Commit 2
pick 895a4b2 Commit 1
...
Use case 8: Auto-resolve conflicts by favoring the working branch version
Code:
git rebase -X theirs branch_name
Motivation: When conflicts arise during a rebase, this command allows you to automatically resolve the conflicts by favoring the working branch version. The theirs
keyword has reversed meaning in this case, favoring the branch being rebased.
Explanation:
git rebase -X theirs
: enables auto-resolution of conflicts by favoring the working branch version.branch_name
: the branch you want to rebase onto.
Example output:
Auto-resolving conflicts by favoring the working branch version.
Conclusion:
The git rebase
command is a powerful tool for reshaping the commit history and incorporating changes from one branch to another. By understanding the different use cases and options available, you can effectively reapply commits, modify commit orders, and resolve conflicts during the rebase process.