0

So typically I'd just delete the remote branch and recreate it however:

  • We have a variety of integrations Hooks etc. that will get messed up if I delete and recreate it.
  • Our repository does not allow git push -f
  • BranchA and BranchB are both based on and several commits ahead of Master

What is the best way to sync remote/BranchA with remote/BranchB

Only way forward I can see is Revert back to whatever similar SHA I can find between the two branches and then doing a merge forward to match BranchA to BranchB but I'd like to get this setup in an script I can execute on demand without having to do a bunch of research to find a similar baseline SHA. Any git magic that I'm missing that might help?

Update

git checkout BranchA
git reset --hard origin/BranchB
git push -f

Would be a way to accomplish this and rewrite history in the process. How do I do it without using git push -f and overwriting history since our repo doesn't allow it.

Update

git merge-base BranchA BranchB

returns the Sha and then

git checkout BranchA
git reset --hard [whateverSha]
git merge BranchB
git push

Does it but It needs to Grep out the SHA first and would still need to push -f.

David
  • 1,131
  • 9
  • 22
  • 1
    `git merge-base` can give desired common hash to you. – Serg Apr 08 '23 at 17:06
  • What does overwrite mean? – matt Apr 08 '23 at 17:11
  • make the resulting directory contents of a git checkout release/alpha byte level identical to a git checkout release/prod without respect to history. – David Apr 08 '23 at 17:15
  • Identical respecting which original? In other words, you want them both to look like one of them now. Which one? – matt Apr 08 '23 at 17:17
  • as stated I'd like release/alpha to be byte level identical to release prod but it really shouldnt matter given they are both children of master if theres magic to do it one way the same magic would work the other way – David Apr 08 '23 at 17:19
  • You keep saying identical to. But that's not what I'm asking. If I have "a" and "b" I can make them identical by making them "b" and "b" or "a" and "a". Which do you want? And no, there absolutely is "magic" but it does _not_ work both ways, _that is why I am asking._ So please answer. – matt Apr 08 '23 at 17:39
  • either. Whatever will do one will do the other. I have branch A based on branch M. I have branch B based on Branch M. If theres a way to make A Identical to B. It should also be able to make B look like A. IF not Im afraid the answer will be suspect. – David Apr 08 '23 at 17:40
  • Updated question to remove what seems to be confusing peeps. – David Apr 08 '23 at 17:47

1 Answers1

0

I would rather make one branch like the other, using the first option I mentioned here

git checkout -b tmp branchB
git merge -s ours branchA         # ignoring all changes from branchA
git checkout branchA         
git merge tmp                        # fast-forward to tmp HEAD
git branch -D tmp                    # deleting tmp

Now, branchA is identical to branchB, and can be pushed (without using --force)

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Right, this is the "magic" (`-s ours`) that I had in mind. But it seems to me that it matters which way you merge, and the OP refused to specify this so I walked away. – matt Apr 08 '23 at 23:54
  • @matt I understand. Let's wait for the OP's feedback. – VonC Apr 08 '23 at 23:55