Search code examples
gitgithubversion-controlgit-merge

New Branch has had many changes - how do I merge into Master?


I've been working on a new branch for a while - I added lots of commits & features to it - so many that the app is completely different from the master branch. What I want is to have my New Branch as master.

Will merging my New Branch into my old master replace the master completely or do I risk mixing up old master code with my New Branch code?

Which commands do I use to assure this works using GitHub as remote repository?


Solution

  • You merge it with the git merge command. That already does the best thing that can be done.

    If you merged from master into your branch regularly during the development, the merge back is trivial and mostly just pushes the state of your branch into master.

    If you didn't, you'll have a lot of conflicts and some wrong guesses about where functionality moved and where it should be merged, but at that point there is nothing any automation could do for you. There is no use crying over spilled renames.

    How do I merge this branch without my data being overwritten or old code being added to this clean branch?

    3-way merge works by finding the most recent common ancestor revision and then applying the changes from both branches to it. It will never overwrite anything with old code on it's own, only if you either tell it to just take one side with the ours or theirs strategy, or do the same thing manually when resolving conflicts.

    If you did merge from master regularly, as is always recommended, this most recent ancestor is the version you last merged, so the ours side is all your development, and the theirs side is at most a couple of simple bugfixes that shouldn't be a problem to apply to your branch. If you didn't merge, it will be the revision you started from and then it depends on how much changes accumulated on master.

    So when you run into conflicts, the key is to still follow the same 3-way merge algorithm, just at higher granularity then the automation is capable of. Try to think about what the code means as little as possible. Just do something like “they added call to sync into this function and we moved the function over there, so the sync call goes there” and you don't really need to think why it should be added.