lve lve - 2 months ago 6
Git Question

Syncing 2 branches after git merge

How do I keep 2 Git branches in sync?

I currently have a

master
branch and a
gh-pages
branch as well as the respective remotes. Both,
master
and
gh-pages
branch shared the same commit id whenever I committed as follows:

git checkout master
git commit -am "message"
git checkout gh-pages
git merge master


My log looked like so (achieved with
git log --graph --oneline --all --decorate
):

single-tracked-graph

After a merge conflict, which I resolved, each merge receives its own commit as such:

double-tracked-graph

My question is, how can I align the 2 branches so that merges will happen as previously on a single 'graph-track'?

I have tried to
pull
according to this answer, but to no avail.

Answer

It looks like commit 50b6dec was added to the gh-pages branch, and commit e43c01f was made on master at the same time, and this is what caused the merge conflict.

Because of that, gh-pages had 1 commit that master did not have. So any time you merged after that, Git was not able to do a fast-forward merge. (The fast-forward merges is what gave you the linear history you had before.) Thus, the solution is:

git checkout master
git merge gh-pages

To prevent this from happening again in the future, change your process to this:

git checkout master
git commit -am "message"
git checkout gh-pages
git merge --ff-only master

The --ff-only flag will abort the merge with an error if gh-pages has any commits that master doesn't have.


If you want to rewrite history so that it is as if this never happened, you can do that too.

First, ensure you are on master:

git checkout master

Now rebase:

git rebase 50b6dec

You'll have to resolve that conflict again. When finished, do:

git rebase --continue

When the rebase is complete, you will have to force-update origin:

git push --force-with-lease origin master

You'll also have to move the gh-pages branch. Do:

git branch -f gh-pages master
git push --force-with-lease origin gh-pages

This will give you a history that looks something like this:

* f218f70' (HEAD -> master) Added Scatterplot functionality to check ratings
* cb79b79' Squashed bug
* fcf0d8a' Small change
* e43c01f' Small change
* 50b6dec Small change
* a217267 Small change

Obligatory warning: Since a rebase rewrites history, this can be dangerous / disruptive for anyone else working on this branch. Be sure you clearly communicate what you have done with anyone you are collaborating with.