Search code examples
gitgit-mergerebase

How to set particular commit to be the 'latest on the main branch'


I've managed to bring my Git repository into a condition which I can't seem to recover from. I'm looking for advice on how I can restore the repository's structe.

  • Commit Fetch comments contains the most up-to-date version of my work. It resides on branch chaos (I called it that out of frustration)

  • Commit Rather large refactor is where the main and chaos branches diverge.

  • Commit Merge pull request #5 from Synovo/send-request is the merge commit between chaos and main. It is by definition ahead of chaos

My goal is to restore (as much as possible) the linearity of the repo, such that changes on the branch chaos are more or less rebased on main.

I mentioned rebasing because my first thought was

  1. Revert to the commit with my changes on it
  2. Rebase onto main

To be honest, I'm not sure why that didn't work. Presumably because the merge commit never really went away, but was reverted.

I then tried resetting

  1. Reset to my commit
  2. Commit those

This left the repo in a fast-forwardable state, which when doing so, returns me to the broken state after the merges.

Importantly, all of my changes are preserved in my commit which I can get to by checking out its hash (compiles, and passes all tests etc, so it's correct)

I've attached a git graph screenshot below to help illustrate my problem.

Git log showing my repository's git history

Before you mention it, yes I know it's a mess, I tried a number of different things to no avail.

The commit labelled Fetch comments is the desired commit. My desired structure looks something like this:

enter image description here

Some advice would be sincerely appreciated!

Btw, the two graphs are in opposite order. Apologies


Solution

  • First, fix chaos:

    git switch chaos
    git reset --hard <sha of fetch comments>
    

    Second, fix master:

    git switch master
    git reset --hard <sha of rather large refactor>
    

    Now just git merge chaos and you will have the desired diagram on your machine. To mirror that up to the remote (GitHub or whatever), you may need to push your branches with force.