How to Configure Rebase and merge (Git)

By default, when performing git pull, a merge commit will be created if the history of the local branch has diverged from the remote one. However, to avoid all these merge commits, a repository can be configured so that it will default to rebase instead of merging when doing git pull. Several configuration targets related to the option are available as follows:

Step 1 : pull.rebase: This configuration, when set to true, will pull to rebase the current branch on top of the fetched one when performing a git pull. It can also be set to preserve so that the local merge commit will not be flattened in the rebase, by passing --preserve-merges to git rebase. The default value is false,as the configuration is not set. To set this option in your local repository, run the following command:

Step 2 : branch.autosetuprebase: When this configuration is set to always, any new branch created with git branch or git checkout that tracks another branch will be set up to pull to rebase (instead of merge). The valid options are as follows:

  • never: This is set to pull to rebase (default)
  • local: This is set to pull to rebase for local tracked branches
  • remote: This is set to pull to rebase for remote tracked branches
  • always: This is set to pull to rebase for all tracked branches

To set this option for all the new branches regardless of tracking remote or local branches, run the following command:

Step 3 : branch.<name>.rebase: This configuration, when set to true, applies only to the <name> branch and tells Git to pull to rebase when performing git pull on the given branch. It can also be set to preserve so that the local merge commit will not be flattened when running git pull. By default, the configuration is not set for any branch. To set the feature/2 branch in the repository to default to rebase, instead of merge, we can run the following command: