gitgithubeluna-lua-engine

GIT - Switch to another remote (Without lose commits)


I've made a clone from a repo, for example : https://github.com/test1/project.git

That's a clone of another repo with a lot of changes (+1000 commits), then I add +200 commits on my local project.

Now, I want to switch to the main repo. https://github.com/main/project.git

I don't want to lose my changes, but I want to remove all commits from test:project in my local project and switch to main:project without losing my own commits and changes.

What is the best way to do this?


Real example :

I made a clone from

https://github.com/ElunaLuaEngine/ElunaTrinityWotlk

Then I made my own changes and commits in my local project.

Now I wanna switch to :

https://github.com/TrinityCore/TrinityCore/tree/3.3.5

And keep my commits and remove all commits from ElunaTrinityWotlk, I don't want to merge commits from ElunaTrinityWotlk to TrinityCore, I just wanna switch to the TrinityCore with only merge my own commits.

ElunaTrinityWotlk is a clone from TrinityCore to add some new features... look at the commits of the ElunaTrinityWotlk, I don't need ElunaTrinityWotlk features anymore wanna switch to TrinityCore branch 3.3.5, I just need to merge my own changes with TrinityCore remove commits of ElunaTrinityWotlk.

enter image description here


Solution

  • OK, when you said you wanted to "switch to the main repo", I had several things in my mind what that could mean. This was not one of them.

    Jeez... this is advanced...

    OK, this is possible, but it won't be simple. Make backups in case you mess up, so you can try again. Well, it shouldn't really be necessary, but with something like this, better safe than sorry.

    TL; DR: Make another GitHub repo forked from Trinity; Locally, in a single repo, fetch commits both from Eluna and Trinity repos; cherry-pick your commits from the Eluna commit tree to the Trinity tree; push back to the Trinity repo.

    In more detail:

    If you're on Windows, I recommend using TortoiseGit. That's my weapon of choice and it's both easy to use and advanced. If you're not in Windows, or you have another tool that you're more familiar with, you can use that. Ultimately this can all be done in command line too - it's just more tedious.

    So, there are several steps here.

    First off, your own github repository is forked from Eluna, so that won't do. Create another one which is forked from Trinity. Leave the first one for now, it'll be a backup in case you mess up and need to start over.

    Then, go to your local copy on your local computer where all the changes live. Make sure all your changes are pushed to your Eluna-based github repository. This is a precaution, so that you don't lose work. Also check that there are no local modifications. For extra safety, clean up all unversioned files too. Make it look like it's a freshly cloned repo with no modifications whatsoever.

    Alternatively, simply make another clone of your Eluna-based github repository in a different folder. Have a fresh start.

    Anyways, next step - make some tags. In particular, tag your own latest commit so that you don't lose it.

    Then - bring in Trinity. In your local repo, create another remote and make it point to your Trinity-based github repo. Fetch the changes from that remote. Don't pull! FETCH.

    Now your local copy will have a mashup of commits from both your Eluna-based repository and your Trinity-based repository, and each will have their own remote.

    Next, make a new branch from a Trinity commit, on top of which you want to move your own commits.

    Now comes the hard part. What you need to do is to cherry pick your own commits, and move them to your new branch. Cherry pick the commits in order, starting with the oldest. In TortoiseGIT you can select multiple commits and cherry pick them all at once.

    Every now and then you will have merge commits where you have pulled the latest Eluna version and merged that into your own code. DO NOT CHERRY PICK THESE. That will just bring the entire Eluna project with it. Only cherry pick your own commits.

    It's quite possible there will be merge conflicts, or perhaps you'll have edited some code that is in Eluna, but isn't in Trinity. In that case - good luck! You'll need to figure out manually how to properly cherry pick that commit.

    Finally though, after some hours (or minutes, or days, depending on what you've done there), you'll have all your commits cherry-picked on top of the latest Trinity commit. Phew!

    Now check that the project still works. After such a long cherry pick, it's more likely that you've missed something than not. Fix that up and commit those changes too.

    In the end, you have a new branch, based off of a Trinity commit, which now also contains all your cherry-picked commits and other changes. Push that to your Trinity github repo. You're done!

    Wipe the local copy and re-clone your Trinity based github repo for a clean slate.