amira
amira

Reputation: 444

Two unmerged repositories inside one project

I have a project that uses modules. One of the modules that I need to use is inside another repository. In the future, I will connect that module into my project using mavenCentral, but not now.

I want to have my MODULE from another repository inside my PROJECT without adding the MODULE into my PROJECT GIT. And I should be able to switch between git repos and commit the changes into their corresponding repositories.

So far I did this:

-- added MODULE origin inside PROJECT like so.

git remote add MODULE [module URL in Github]
git fetch MODULE

after the above steps, when I do git remote -v it is displaying two remotes. One for PROJECT (origin) and one for MODULE.

After these steps, I am not sure what to do next in order to achieve my objective. Is it even possible?

I tried git merge MODULE/master --allow-unrelated-histories but it is adding my MODULE code into my PROJECT which I don't want.

Could someone point me in the right direction? Thanks.

Upvotes: 2

Views: 125

Answers (2)

EJV
EJV

Reputation: 31

The answers to this question put me in the right direction, but I ran into a few things that aren't explained here. I'd like to share my experience in case anyone else needs it.

My goal was to be able to add a library or module to my current project from a Github repository and be able to manage the VCS from Android Studio with both Github repositories at the same time, one for the project itself and one for the library.

The first thing I did was add the git submodule as explained in this thread, but I did it using the Android Studio terminal:

git submodule add MODULE_URL

Once Git added the module from Github or wherever, the next thing we do is add the module to the project as we would do with any module in Android Studio. To do this, in the settings.gradle.kts file, which is the file that contains the settings for the currently open project, I add the module by adding the line:

include(":MODULE_NAME")

With this, gradle will recognize and compile the module that was just added. Now we only need to include the module in our project as such so that the app has access to the lib. To do this, in the build.gradle.kts file of the app module, I add this line so that the module is compiled as another dependency in the app:

implementation(project(":MODULE_NAME"))

Up to this point we have already added the module to the project. Once I got to this point I realized that Android Studio did not recognize the two repositories and that apparently I was going to have to commit and push the module manually through the terminal, but Android Studio does recognize and allows working with different modules hosted in different repositories, the only thing to do is simply restart Android Studio and then it will recognize the two repositories and handle the entire commit and push process by itself. That's all, enjoy!

Upvotes: 3

VonC
VonC

Reputation: 1328082

It would be easier to use git submodule for that kind of use.

Your parent repository would keep a reference on your MODULE repository in a MODULE subfolder, but any change done on that subfolder can be pushed directly to the remote MODULE repository.

cd /path/to/repo
git submodule add [module URL]
cd module
# work on module, add, push, commit
cd ..
git add module
git commit -m "reference new module state"

The OP amira confirms in the comments:

Worked like a charm.
In the settings.gradle folder, don't forget to include the module and provide a path as described in "Android Studio - How to make modules inside a subdirectory?".

Upvotes: 2

Related Questions