-3

First of all, I've tried to look for this before, but only "How to revert merges" post are shown, so I write this in hope to solve the problem.

What happened:

  • I have a branch (Father)
  • A job friend made a new branch from father to start working there.
  • I start coding and pushed to father branch instead new one some commits (my bad)
  • Another person revert commits to solve the problem, so I can push them to the correct branch.
  • A month later, we finish working on said branch, and now I need to merge this branch back to father.

The problem:

  • When I do the merge, the father reverted commits are being applied, deleting/downgrading part of my job.

Important to know

  • Father branch remains untouched (Just the reverts have happened after we create the new branch)

What I need

  • Just merging branch to father without loosing part of my already done job. All changes from my branch should remain.

What Ive tried

  • Merge father to child (Didnt work, thats the initial problem, it brings reverts and undo job)
  • Made some changes to the files which were reverted (on child branch), so update date is newer so they should remain now (Didnt work, only keep deleted files, but the rest is still being undo)
  • Merge child to father (Didnt work, reverted commits are still aplying)

What may solve the problem but doesnt look like a good option

  • Cherry pick, one by one, all commits from child to father (61 commits including some merges from other branches from child)
  • Copy files that could be reverted, merge, paste files back, commit and push (I dont even think this should be a option, but still, may work)

Don't know what else to do or how to solve this problem correctly. We need to deploy the app to production soon, and need to fix this issue in order to do that.

A-B-C-D-E-F-Dr-Er-Fr (Branch A)
     \
      D-E-F-G-H-I.... (Branch B)

Dr, Er, Fr are the reverted commits

As you can see, they're added in the new branch, but have the same date as branch A, because after reverting, I reseted the Branch A so I have the changes staged. Checked out Branch B, and committed/pushed changes here.

RushTfe
  • 5
  • 3
  • 1
    Please don't be offended, but did you read https://github.com/git/git/blob/master/Documentation/howto/revert-a-faulty-merge.txt, the classic explanation of what happens when a merge commit is reverted? Because if I'm reading you correctly, yours is exactly that case: I merged, my merge was reverted, now I'm merging again and my old commits are not getting applied. The article totally explains this and tells you what to do. – matt Sep 06 '21 at 11:55
  • 1
    From where on father did your new branch start? Before your original work on father that was reverted? After that work, on father, but before the reverts were made, or after the reverts were made on father? Ie. you have the following history: C1-W-R-C2 (W = the work you made on father, that you shouldn't have, and R = the revert commit(s) for W). Is your branch from before W, between W and R or after R? – Lasse V. Karlsen Sep 06 '21 at 11:57
  • @LasseV.Karlsen yes, branch started before my commits were reverted. In order, branch creation -> push to wrong branch -> revert changes in father -> push changes now to correct branch (Date is before reverting, because i didnt rewrite everything, but picked it from git) – RushTfe Sep 06 '21 at 12:01
  • 1
    "push changes now to correct branch", what exactly does this mean? You can't push changes onto branches, you make commits on branches and you push to remote repositories. If your commits were already on the father branch, what did you do in order to get them onto a new branch? Please be specific in the commands or operations you did here as the answer that you require depends on exactly what you did. – Lasse V. Karlsen Sep 06 '21 at 12:03
  • @LasseV.Karlsen After reverting changes, reset branch to get the changes staged for commit, changed branch to child, commit and push changes here. It was a month ago, I can't tell 100% it was what I did, but should be really similar, because the dates of the files on the child branch are from BEFORE the reverts – RushTfe Sep 06 '21 at 12:08
  • 1
    Is there any chance you can visualize what the commits look like, where your changes was introduced, duplicated, reverted, etc.? I'm still having a hard time understanding exactly what your history graph would look like. – Lasse V. Karlsen Sep 06 '21 at 12:19
  • It seems I could fix it, what I did: Rebase father onto child, so all my changes from child are now in father, but most important, I used the option --interactive so I could avoid the commits with the reverts created after the new branch was created. – RushTfe Sep 06 '21 at 13:06

1 Answers1

0

I finally was able to fix it, and was mostly easy.

Just neded to revert the reverted commits and push the changes. Now I'm able to merge my branch normally.

Reading what matt said on first place gave a good point of view to. That problem was about reverting a merge already pushed (Not exactly my case) but was a really helpful reading, can recommend it to everyone.

https://github.com/git/git/blob/master/Documentation/howto/revert-a-faulty-merge.txt

RushTfe
  • 5
  • 3
  • If this is not the answer, do not put it in an Answer field. – matt Sep 06 '21 at 15:06
  • "Problem is that when you are going to push your changes, you need to pull the commits you avoided from origin" - this is the error message giving you unhelpful advice. When two branches have diverged, you have *two* options: merge them together, or throw one away and overwrite it with the other. Read up on "force push", and be sure you understand the risks and side effects; it is probably what you want here, but it's often not, which is why git doesn't like to draw attention to it. – IMSoP Sep 06 '21 at 15:47
  • Please provide additional details in your answer. As it's currently written, it's hard to understand your solution. – Community Sep 06 '21 at 18:32
  • @matt, when I wrote it, it was the solution, now I can't unmark it as solution. – RushTfe Sep 07 '21 at 07:34
  • @IMSoP, you're probably right, I'll give it a try, ty! – RushTfe Sep 07 '21 at 07:35
  • 1
    Yes you can, just unmark it. But if it's wrong then even better, delete it. – matt Sep 07 '21 at 07:36