Ben Lobaugh Online

I think, therefore I am. I am, therefore I sail

Replace one git branch with the contents of another

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

git checkout prod
git checkout -b new-dev
git merge -s ours dev
git checkout dev
git merge new-dev
git push dev

Breakdown

git checkout prod
git checkout -b new-dev

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

git merge -s ours dev

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.

git checkout dev
git merge new-dev
gitpush dev

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.

Previous

Missing the sunny summer sailing days

Next

Lovely afternoon sail

5 Comments

  1. Jean-Yves

    THanks, you just saved me hours of pain – I needed exactly the same process 🙂

  2. Just wanted to correct a slight syntax error in the final step of the breakdown. You’re missing a space between `git` and `push`

  3. Josh

    Bless you sir. Bless you.

  4. Well done, sir. I have two recommendations:

    1. Name branches “source” and “target” to make it more clear which way the content is flowing.
    2. Remove the interims branch “new-dev” when you’re done.

  5. Trai Tran

    Thank you so much! It very useful for me and solved fast my problem.

Leave a Reply

Your email address will not be published. Required fields are marked *

Powered by WordPress & Drip City Coffee