How to change Git subtree merge source

I have a project in which I have combined in a library using the Git subtree. I clicked and pulled out a few small changes between the library and the project.

A new repository was later created, which is the ultimate home for the library. It contains essentially the same version of the library code as my project, possibly with one or two minor changes. For various reasons, it does not convey any direct history of Git with the previous library house (this is not a clone of the previous library).

Now I want to change the project so that it pulls / pushes the library from a new location. The first time this happens, I also need to resolve any merge conflicts, although in this case the changes are trivial and you can simply redo them later.

What is the best way to do this?

I tried to delete the library instance in my project, and then delete it along with the old remotes and branches. Then I tried to add a subtree, etc. From a new location. It seems to have worked, but when I try to push off my project in the library, I get a deadly error with a bad object.

I assume that there is a flaw in the approach I tried - perhaps this is due to the lack of a common history - but I do not have a deep understanding of what is happening to know how to fix it, or what is the β€œright” approach to this problem it should be.

[update: edited the question to make it a little clearer - it was a bit ambiguous)

+4
source share
3 answers

Try cloning a new project. then adding yours as deleted and making

git remote update

This will remove all links from your project. now do

git cherry-pick <sha1>

the step you want in the new project. This is the easiest way, I think.

You should also know that git does not require a common history (although it does a less pleasant merge for merging) for merging. so that you could just do what I said, and then instead of picking cherries you could combine your story. this is likely to have conflicts. I suggest, as soon as you start merging (if you do) use git mergetool and learn how to use 3-way diff.

+1
source

Is there a reason why you are not referencing your library as a submodule from your project?
(See the true nature of submodules )

The advantage of your case would be how easy it is to change the address of the submodule .

+1
source

Perhaps you need to create a separate working branch (or repository) with your version of the library (only the library tree with your changes), which could then be returned to the original repo library.

So, to go to the original remote repo, first prepare locally what you expect to appear as a commit remotely; There can be different workflows to achieve this: either while working on a project, you first make changes to your library in your special local library work branch (this branch should inherit the original library history), and then merge this branch with the library changes to your working branch of the project (where the library is a subtree) or merges your changes back from the working branch of the project into the working branch of the selected library. Then you can direct your specialized library work branch to the original remote repo if you want.

So, essentially, first you create a local dedicated working branch so that it inherits the original history:

 git branch MY_LIBFOO REMOTE_BRANCH_LIBFOO 

(REMOTE_BRANCH_LIBFOO is your local remote branch that interests you, which is updated by git fetch)

then, following your workflow, make sure your changes to the library are in the MY_LIBFOO work branch, and then you can

 git push original_libfoo MY_LIBFOO:TARGET_BRANCH_REMOTELY 

Thus, there is a clear idea of ​​what is happening.

+1
source

Source: https://habr.com/ru/post/1315054/


All Articles