You have divergent branches

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:

  1. You have your local commits.
  2. There are commits on the remote branch.
  3. Git creates a new merge commit that combines both sets of changes.

Pros:

  1. Retains the complete history of how changes were made.
  2. Easier to understand the sequence of development, including when merges occurred.

Cons:

  1. 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:

  1. Your local commits are temporarily set aside.
  2. Git applies the commits from the remote branch.
  3. Your local commits are reapplied on top of the remote commits.

Pros:

  1. Results in a cleaner, linear history.
  2. Easier to follow the evolution of the project without merge commits.

Cons:

  1. Rewriting history can be confusing, especially if conflicts need to be resolved during the rebase.
  2. 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:

  1. Your local branch is checked to see if it can be fast-forwarded to the remote branch.
  2. If there are no local commits that diverge, Git simply moves the branch pointer forward.

Pros:

  1. Keeps the history clean without any merge commits.
  2. Ensures you only update if no local work would be lost.

Cons:

  1. 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 🙂

https://behindmethods.com

Ankush 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.


Why Clients Trust Us

Since our establishment in 2011, we’ve maintained an impeccable track record of success, proudly serving a diverse clientele in the USA and Canada. What sets us apart is our close-knit team of family and friends, fostering a stable and dependable environment. Unlike many companies where programmers and developers come and go, our commitment to delivering innovative and high-quality solutions remains unwavering. Our clients trust us not just for immediate needs but as their enduring partner for long-term success.

Stay Connected

Copyright © Behind Methods 2023-24. All rights reserved. | Privacy Policy | Terms & Conditions

Logos depicted are copyright of the respective companies, and shown here only for illustrative purpose.

Customer Rating

based on number of successful completed jobs on Upwork and across various IT verticals.