Git rebase¶
Git rebase is a version control operation used to integrate changes from one branch into another by re-applying commits on top of a new base commit.^[600-developer-tools-git-git-rebase.md]
Basic Workflow¶
The standard workflow for integrating a feature branch (e.g., issue) into a master branch using rebase involves updating the master branch first, then applying feature commits onto it.^[600-developer-tools-git-git-rebase.md] The typical steps are:
- Checkout the feature branch (
git checkout issue) and complete development.^[600-developer-tools-git-git-rebase.md] - Checkout the master branch (
git checkout master) and pull the latest changes (git pull).^[600-developer-tools-git-git-rebase.md] - Return to the feature branch (
git checkout issue).^[600-developer-tools-git-git-rebase.md] - Execute
git rebase masterto set master as the new base for the feature branch.^[600-developer-tools-git-git-rebase.md] - If conflicts occur during the rebase, resolve them and use
git rebase --continueto proceed.^[600-developer-tools-git-git-rebase.md] - Stage resolved files (
git add .) and commit the changes.^[600-developer-tools-git-git-rebase.md] - Switch back to master (
git checkout master) and merge the feature branch (git merge issue).^[600-developer-tools-git-git-rebase.md] - Finally, delete the feature branch (
git branch -d issue) and push the updates.^[600-developer-tools-git-git-rebase.md]
Error Handling¶
If a rebase operation results in an error—such as inadvertently rebasing the master branch onto a feature branch—it can be undone using the reset command: git reset ORIG_HEAD --hard.^[600-developer-tools-git-git-rebase.md]
Rebasing Without Feature Branches¶
In scenarios where developers are working directly on a shared branch without using feature branches, rebase is used to synchronize local changes with the remote repository.^[600-developer-tools-git-git-rebase.md]
Manual Rebase¶
The manual process involves fetching upstream changes and rebasing the local branch onto top of them:^[600-developer-tools-git-git-rebase.md]
git fetchgit rebase- Resolve conflicts, then
git add [conflicted files]. git rebase --continuegit push
Pull with rebase¶
A more streamlined approach is to use the pull command with the rebase flag:^[600-developer-tools-git-git-rebase.md]
- Modify code and commit changes.
git pull --rebasegit push
Configuration¶
To avoid typing the --rebase flag every time, Git can be configured to use rebase by default for git pull operations.^[600-developer-tools-git-git-rebase.md]
Per-Branch Configuration¶
To enable rebase for a specific branch (e.g., master):^[600-developer-tools-git-git-rebase.md]
git config branch.master.rebase true
Global Configuration¶
To enable rebase for all branches or set up rebase automatically for new branches:^[600-developer-tools-git-git-rebase.md]
// Set rebase to true for all branches
git config --global pull.rebase true
// Automatically set rebase to true for new branches
git config --global branch.autoSetupRebase always
To unset these global configurations:^[600-developer-tools-git-git-rebase.md]
git config --global --unset pull.rebase
git config --global --unset branch.autoSetupRebase
Related Concepts¶
- [[Git]]
- [[Git merge]]
Sources¶
^[600-developer-tools-git-git-rebase.md]