Divergent branches reconciliation
When working with local and remote branches in Git, you might encounter this error when pushing or pulling from the remote (GitHub).
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.
fatal: Need to specify how to reconcile divergent branches.
What happened?#
- Your local branch and the branch on GitHub (remote branch) have different changes.
- Git doesn't know how to combine (reconcile) these changes.
Why it matters?#
- You need to decide how to merge these changes so both branches are up to date.
How to fix?#
1. Merge#
Combine changes and keep all commits (default strategy).
git pull --no-rebase origin main
#or
git pull origin main
2. Rebase#
Move your changes on top of the remote changes, creating a straight line of commits.
git pull --rebase origin main
3. Fast-forward#
Only update if there are no new local changes.
git pull --ff-only origin main
Summary#
Choose a strategy (merge, rebase, or fast-forward) to update your branch.