You have divergent branches
hint: You have divergent branches and need to specify how to reconcile them.
hint: You can do so by running one of the following commands sometime before
hint: your next pull:
hint:
hint: git config pull.rebase false # merge
hint: git config pull.rebase true # rebase
hint: git config pull.ff only # fast-forward only
hint:
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories. You can also pass --rebase, --no-rebase,
hint: or --ff-only on the command line to override the configured default per
hint: invocation.
Introduction
When working with Git, you might encounter a situation where your local branch has diverged from its remote counterpart. This means that both branches have commits that are not in the other and this is called divergent branches. To reconcile these differences, you have a few options for how you want Git to handle the integration of these changes when you perform a git pull
.
The hints provided give you three main methods for reconciling divergent branches: merge, rebase, and fast-forward only. Here’s a detailed explanation of each method to fix divergent branches.
1. Merge (git config pull.rebase false)
When you pull with merging, Git will create a new “merge commit” that integrates the changes from the remote branch with your local branch. This preserves the history of both branches as they were before the pull.
Example: git config pull.rebase false
Workflow:
- You have your local commits.
- There are commits on the remote branch.
- Git creates a new merge commit that combines both sets of changes.
Pros:
- Retains the complete history of how changes were made.
- Easier to understand the sequence of development, including when merges occurred.
Cons:
- The history can become cluttered with many merge commits, especially in a busy project.
2. Rebase (git config pull.rebase true)
When you pull with rebasing, Git will reapply your local commits on top of the changes from the remote branch. This creates a linear history without merge commits.
Example: git config pull.rebase true
Workflow:
- Your local commits are temporarily set aside.
- Git applies the commits from the remote branch.
- Your local commits are reapplied on top of the remote commits.
Pros:
- Results in a cleaner, linear history.
- Easier to follow the evolution of the project without merge commits.
Cons:
- Rewriting history can be confusing, especially if conflicts need to be resolved during the rebase.
- Can be problematic for shared branches if not all collaborators understand rebasing.
3. Fast-Forward Only (git config pull.ff only)
When you pull with fast-forward only, Git will update your branch to match the remote branch only if your branch can be fast-forwarded. This means that your local branch must be directly behind the remote branch with no local commits. If this is not possible, the pull will fail.
Example: git config pull.ff only
Workflow:
- Your local branch is checked to see if it can be fast-forwarded to the remote branch.
- If there are no local commits that diverge, Git simply moves the branch pointer forward.
Pros:
- Keeps the history clean without any merge commits.
- Ensures you only update if no local work would be lost.
Cons:
- Will fail if your branch has diverged with local commits, requiring manual intervention.
Setting Default Preferences
You can set a default behavior for all repositories or just for the current one using the git config
command. If you want to set a global default preference (affecting all repositories on your machine), use --global
:
Example: git config --global pull.rebase true
Overriding Configurations Per Invocation
Regardless of the default configuration, you can override the behavior for a specific pull command using the following options:
--rebase
to force a rebase:
Example: git pull --rebase
--no-rebase
to ensure a merge:
Example: git pull --no-rebase
--ff-only
to require a fast-forward merge:
Example: git pull --ff-only
Summary
- Merge (
pull.rebase false
): Creates a merge commit, keeping the full history but adding clutter. - Rebase (
pull.rebase true
): Reapplies local changes on top of the remote branch, resulting in a linear history but rewriting history. - Fast-Forward Only (
pull.ff only
): Updates the branch only if it can be fast-forwarded, failing otherwise to avoid merge commits or rebases.
Choose the method that best fits your workflow and project requirements.
Happy Coding 🙂
Ankush Shandilya
https://behindmethods.comAnkush is in India's top 3.5 talents associated with Uplers and is a co-founder of Behind Methods. He is seasoned Full Cycle developer with over 15 years of experience in crafting custom web applications. With two master's degrees in hand, his journey in the world of web development has been a thrilling and rewarding experience. He just doesn't build applications but collaborate closely with clients to understand their unique needs and challenges. This allows him to tailor solutions that align perfectly with their business objectives and help them navigating their digital landscape and achieve their business goals. Some of his awesome projects are PerkZilla, CoinDiscovery, 24Lottos, Zen Flowchart and MoverWise Software.