vim, git, aws and other three-letter words

Software Development Notes

git: your branch and 'origin/xxx' have diverged error

Problem: Your branch and origin/branch have diverged

The Git error looks like this:

Your branch and 'origin/xxx' have diverged,
and have 1 and 1 different commit(s) each, respectively.

The error is caused by two independent commits - one (or more) on the local branch copy and another - on the remote branch copy (for example, commit by another person to the same branch).

Another case for the error is git rebase (error is expected, see below).

The history looks like this:

... o ---- o ---- A ---- B  origin/branch_xxx (upstream work)
                   \
                    C  branch_xxx (your work)

The solution depends on what has actually happened, the reason why the upstream state has changed. If someone else is also working on the same branch, a good way to solve it is to rebase the commit C on top of the remote state:

git rebase origin/branch_xxx

The history after the rebase will look like this:

... o ---- o ---- A ---- B  origin/branch_xxx (upstream work)
                          \
                           C`  branch_xxx (your work)

Another way to fix the issue is to merge the upstream branch state to local:

git merge origin/branch_xxx

The history after the merge will look like this:

... o ---- o ---- A ---- C ---- B  --- [merge commit]

The same git error after rebase

This happens if you rebase the branch which was previously pushed to the origin repository, for example, we start with a state like this:

... o ---- o ---- A ---- B  master, origin/master
                   \
                    C  branch_xxx, origin/branch_xxx

Now we want to rebase branch_xxx against the master branch:

git checkout master
git pull
git checkout branch_xxx
git rebase master

Now the local state of branch_xxx and origin/branch_xxx will diverge. This is expected as rebase rewrites history, so after it you'll have different local and remote state:

... o ---- o ---- A ---------------------- B  master, origin/master
                   \                        \
                    C  origin/branch_xxx     C` branch_xxx
git status
On branch branch
Your branch and 'origin/branch' have diverged,
and have 1 and 1 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)

If you are absolutely sure this is your case then you can force Git to push your changes:

git push -f origin branch_xxx

Tags: git