Skip to content

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:

  1. Checkout the feature branch (git checkout issue) and complete development.^[600-developer-tools-git-git-rebase.md]
  2. Checkout the master branch (git checkout master) and pull the latest changes (git pull).^[600-developer-tools-git-git-rebase.md]
  3. Return to the feature branch (git checkout issue).^[600-developer-tools-git-git-rebase.md]
  4. Execute git rebase master to set master as the new base for the feature branch.^[600-developer-tools-git-git-rebase.md]
  5. If conflicts occur during the rebase, resolve them and use git rebase --continue to proceed.^[600-developer-tools-git-git-rebase.md]
  6. Stage resolved files (git add .) and commit the changes.^[600-developer-tools-git-git-rebase.md]
  7. Switch back to master (git checkout master) and merge the feature branch (git merge issue).^[600-developer-tools-git-git-rebase.md]
  8. 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]

  1. git fetch
  2. git rebase
  3. Resolve conflicts, then git add [conflicted files].
  4. git rebase --continue
  5. git 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]

  1. Modify code and commit changes.
  2. git pull --rebase
  3. git 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

  • [[Git]]
  • [[Git merge]]

Sources

^[600-developer-tools-git-git-rebase.md]