Search code examples
gitgit-submodules

How to add a git submodule from other repo and merge it with local folder


I have a specific situation and i didn't found the answer on stack that matches it.
I have a root_project which is base app directory, synchronized with it's remote repo.

Now i have a folder in it, which is named oscar. I've previously downloaded zipped oscar and copied it into my root_project, after i've customised it in places i needed.
After that i have noticed that i get into troubles if i need to get updates from remote oscar once those are made up.

The basic idea is to use git submodule but really can't figure out how to adapt it to my necesities as i don't want to crash my project and repo on experimenting.

Also a specific part is that i need only src/oscar from oscar repo, that is actually in my root_project and to make it possible to merge with my changes in local oscar folder, without loosing the things i've already done locally.

I would appreciate any help and suggestions on how to use correctly git submodule in current circumstances, also if possible wanna see the steps i must do on achieving desired results


Solution

  • [...] as i don't want to crash my project and repo on experimenting.

    First of all, you should not be afraid to try things in your local repository. As long as you stay on your local copy and do not publish experimental changes, you can always just delete the whole thing and clone it again from your remote. It is a very good way to learn stuff if you just try things out, especially when you break everything underway. Do not hesitate to do so!

    To answer your question: I assume oscar exists as a remote repository. Git submodules are indeed a good way to manage a subrepository that you need to update occasionally. You can find good documentation on how to use submodules here. It will be easier if you clone oscar directly from the remote via git submodule add instead of downloading and extracting a zip file first. This newly cloned submodule will of course not contain your local changes to oscar, but you can easily import them by following the steps described here.

    Also a specific part is that i need only src/oscar from oscar repo, that is actually in my root_project and to make it possible to merge with my changes in local oscar folder, without loosing the things i've already done locally.

    I do not understand this part. Do you only want to add a subfolder of oscaras a submodule?

    EDIT: So you want your submodule to conatin only a subfolder of the oscar repository. You can do this after cloning the whole repository (via git submodule add) with

    git filter-branch --prune-empty --subdirectory-filter FOLDER-NAME  BRANCH-NAME 
    

    The process is described here, although only point 5 is of interest for you. Note that you have to do this inside your submodule, i.e. you have to cd into your submodule folder.