Wednesday, January 22, 2025

Setting Git Merge Preferences

 In Git, you must configure how you want to handle merge conflicts when pulling changes. The most common options are merge (default) and rebase. Here's how to set a merge preference, with examples:


What Are the Options?

  1. Merge: Combines the remote branch into your local branch using a merge commit.
  2. Rebase: Applies your local changes on top of the remote branch's changes, creating a cleaner commit history.

Set Merge Preference Globally

Option 1: Set Default to Merge

To set the default behavior to merge, run:


git config --global pull.rebase false

Example:

If you pull from a remote branch, Git will create a merge commit:


git pull origin main

Git creates a commit like:


Merge branch 'main' of <remote repository> into <local branch>

Option 2: Set Default to Rebase

To set the default behavior to rebase, run:


git config --global pull.rebase true

Example:

If you pull from a remote branch, Git will reapply your commits on top of the remote branch:


git pull origin main

Git rewrites the history, creating a linear commit tree.


Set Merge Preference Per Repository

If you want to set preferences for a specific repository:

Example:

  1. Navigate to the repository directory:


    cd /path/to/your/repository
  2. Set merge as the default:


    git config pull.rebase false
  3. Or, set rebase as the default:


    git config pull.rebase true

Set Merge Preference Temporarily

You can override the default behavior for a single pull command:

  • Merge:


    git pull --no-rebase
  • Rebase:


    git pull --rebase

Check Your Current Preference

To check the current pull behavior, run:


git config --global pull.rebase

Example Output

If you run:


git pull origin main
  • With pull.rebase=false:

    Merge made by the 'recursive' strategy.
  • With pull.rebase=true:

    Successfully rebased and updated refs/heads/main.


No comments: