3

I have an issue with my branches

Introduction

I have 3 branches on my project : Dev, Main & Staging

On Dev branch we add new features, on Main branch we fix bugs, on Staging we deploy for testing.

When a bug PullRequest is completed on Main branch, we make a PullRequest Main -> Dev** to keep up to date the Dev branch. When a new feature is developed, a PullRequest is complete on Dev branch.

When we want to deploy all the new features, we make a PullRequest Dev -> Main, and then a PullRequest Main -> Staging. Finally we deploy the content of Staging branch

Problem

When I compare manually my branches Dev and Main, I see there are differences : in Dev branch some files appeared in a folder, but it were moved to another folder before in a previous commit on Main branch.

When I make git diff Main..Dev, I see there are same differences than above.

Normally, at this moment, this two branches have to be at the same state. So I made a PullRequest Main -> Dev to give to branch Dev the correct state (Main state) : but it say that there are no changes on my PullRequest.

Question

How can I properly give to Dev branch the current state of my Main branch ?

Thank you

Nathan Bruet
  • 328
  • 1
  • 5
  • 17
  • ", I see there are differences.", could you tell us a little bit of **what kind** of differences you see? – Lasse V. Karlsen Jun 02 '21 at 08:43
  • I update the question to add more precision for sentence ", I see there are differences." – Nathan Bruet Jun 02 '21 at 08:48
  • How does a pull request end? Merge, rebase, or squash and merge? – matt Jun 02 '21 at 09:00
  • if you want the exact same state you don"t want to merge you want to force the branch to be the same. you can have reach this state by having fixed some conflict on merge leading to this difference, after that on merge you will get this resolution. – Ôrel Jun 02 '21 at 09:24
  • @matt : It's "Merge (No fast-forward)" – Nathan Bruet Jun 02 '21 at 09:59
  • @Ôrel : How can I force the branch to be the same ? – Nathan Bruet Jun 02 '21 at 09:59
  • What you are describing is what I would expect to see if you tried to merge Main to Dev twice in a row. They differ, but there is nothing to do. – matt Jun 02 '21 at 10:16
  • "How can I force the branch to be the same" That is not what a merge is at all. Merges don't cause two branches to represent the same state of the project. If you wanted Dev to be absolutely identical to Main you would branch Dev off of Main! – matt Jun 02 '21 at 10:21
  • @matt I don't want that Dev to be identical to Main (with same commit, etc..). I just want that Dev have the same files than Main just at this moment (not every time). I have one solution : copy all files of Main branch, checkout to Dev, paste all files. But I don't like this solution, I would like to know if there is another cleaner solution – Nathan Bruet Jun 02 '21 at 12:46

1 Answers1

2

What might have happened

As @matt said in the comments, this is probably the result of a previous merge, where the conflict might have been resolved without moving the files. Now, when you merge again, Git thinks it's already handled that renaming so it doesn't have to deal with it. You could confirm this theory by looking at the commit history since the last merge point: are the renames before or after that?

How to merge Main in and force Dev to take the state of Main

If, at this point, you want to throw away the current state of Dev and make it exactly the state of Main, and you want to do with with a merge operation, I would use the ours strategy. I'm not sure you can do this via a PR, you'll probably have to do it on your PC and push, I hope your workflow allows this!

Unfortunately, you cannot directly do git merge -s theirs Main from Dev, that would be too simple. You have to do git merge -s ours Dev from Main to create the merge commit you want on Dev:

git checkout Main
git merge -s ours Dev  # this "merges" Dev in but ignores all its changes
# don't push this!
git checkout Dev
git merge Main  # this should be a fast-forward merge
# now you can push Dev

And you can clean up your sandbox when you're done, by bringing Main back to where it was, since that merge was intended for Dev:

git checkout Main
git reset --hard origin/Main

What if I want the renames from Main, but not lose the other changes in Dev?

In this case I think you will need to do some manual work. If the renames are indeed before the previous merge, and the merge that brought those commits into the history of Dev was resolved without applying the renames, you need to recreate the renames again on the Dev branch, possibly by hand.

I don't imagine you have the option of going back to that earlier merge and redo it correctly, but if you could, that might be the cleanest option.

joanis
  • 10,635
  • 14
  • 30
  • 40
  • Thanks for your solution, it works ! I don't know the parameters "-s ours" of the merge command, thank for the discovery ! – Nathan Bruet Jun 03 '21 at 08:18