Replace one git branch with the contents of another

December 21, 2015

At WebDevStudios we use branches as deployment channels in Beanstalk. Today I ran across a situation where one of the dev branches had significantly diverged from the production branch. difftool showed 988 differing files. Needless to say this caused some issues on the dev server.

I needed to sync the dev branch with the production branch by replacing everything in the dev branch. Here is how I did it:

All at once

[bash]
git checkout prod
git checkout -b new-dev
git merge -s ours dev
git checkout dev
git merge new-dev
git push dev
[/bash]

Breakdown

[bash]
git checkout prod
git checkout -b new-dev
[/bash]

Create a new branch off of the production branch that can merges can be tested in.

[bash]
git merge -s ours dev
[/bash]

using -s ours will essentially overwrite everything from dev with changes in new-dev (prod originally). If this switch is not used a lot of conflicts would have been introduced and I do not have enough scotch on hand to stop that headache.

[bash]
git checkout dev
git merge new-dev
gitpush dev
[/bash]

It is now safe to merge new-dev into dev. There should not be any conflicts. Push it out and the dev branch is now sync’d back with prod.