0

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?

TylerH
  • 20,799
  • 66
  • 75
  • 101
Sunshine
  • 372
  • 2
  • 21
  • 1
    As soon as you ask "should", "can" or ask for a "best practice" your question is inherently opinion based, which is off-topic for SO. – fredrik Aug 06 '21 at 08:29
  • I see sorry is it better now ? I really don't know what to do... where can I get an answer I don't want to mess up my code : / – Sunshine Aug 06 '21 at 08:39
  • It is still basically the same question - which is opinion based. How you resolve the issue is up to you - there is no best way. – fredrik Aug 06 '21 at 08:57
  • ... everyone uses git there must be an industry standard - I'm not looking for a complicated answer just how to solve my issue - > which is merging a branch with lots of changes without my data being overwritten – Sunshine Aug 06 '21 at 09:16
  • 2
    Your "data being overwritten" is strange in the context. You're using git. There's no "overwriting". Make your branch your new remote or merge it into master, both technically achieve what you want. Keep a reference on your old master and you'll be safe if anything goes awry. – Romain Valeri Aug 06 '21 at 09:30
  • I've updated question - are you sure old data from my old master won't be mixed up with my clean code from my working branch that will now be master ? How do I do it ? – Sunshine Aug 06 '21 at 09:40
  • 2
    Side note: I gather English is not your first language :-) The phrase "working branch" is a bit tortured: Git has a *working tree*, but a *current branch* rather than a *working branch*. There's no particularly good reason for this change of adjective. English is just weird. – torek Aug 06 '21 at 13:59
  • Okay thks a lot - i’ve corrected question – Sunshine Aug 06 '21 at 14:13

1 Answers1

1

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.

Jan Hudec
  • 73,652
  • 13
  • 125
  • 172
  • My Hero ! thks so much for this explanation ! - I thought I should merge only when my feature was complete - it took so many commits to finish it & I worked on other things along the way - should I have worked only on the feature on this branch & polished my app on the master ? – Sunshine Aug 06 '21 at 12:19
  • Also do you recommend using this steps for my situation ? [merge a branch into a master steps](https://stackoverflow.com/a/40420874) – Sunshine Aug 06 '21 at 12:21
  • @IhitaNaidu, that's the standard workflow, unless you are using some repository manager with merge (pull) requests. Just make sure you test the result of the merge before the final push, especially if you have a lot of conflicts to resolve. – Jan Hudec Aug 06 '21 at 14:31
  • … you can also push back to the branch, or to some other branch (you can explicitly specify the target branch for the `push` command; read the manual) before pushing to master if you have some tests to be executed on or by a build server. – Jan Hudec Aug 06 '21 at 14:32