2

Update:
I think I may have found one possible solution for this question: git worktree. I'll have to investigate its exact workings a bit more, because I'm not entirely sure yet if it can do what I want (particularly automatically keeping synced). But if it's viable, I'll answer my own question with my own solution.


I might be going about this in an unorthodox way and that's why I'd like to get some input on alternative ideas.

I have created a Java Promise library, that is in ongoing development. I've included this library into an Android app that I am currently developing as well. At this point only my Promise library is version controlled with Git.

I only develop for myself, locally, so there's no worries about other people's commits or remote repositories.

Here's the basic local structure:

  • /Library/Promise/.git
  • /App/MyApp/

Right now, when I want do some work on the Promise library, by checking out, say, the "develop" branch, this change is immediately reflected in MyApp's code as well1. I'd like to prevent this from happening. I'd like to only include a version of my Promise library that I am currently happy with.

For instance, only include:

  • the master branch
  • a tagged commit
  • etc.

In the meanwhile I've actually found a way to achieve what I want, when a repository is hosted on Github. But I'd like to achieve this exact same thing with a local git repository.


Is there a convenient way to only include a snapshot of the library's Git code, without having to make a copy of the library? Or is it typically not custom to directly include a Git repository into another project, like this?

Preferably, I'd like to stay away from advanced dependency repositories, like Maven and what have you, for now, as that's a bit too much much hassle for me, at the moment. I'm hoping there are some less involved alternatives for this conundrum.


  1. To address @candied_orange's comment:

    I develop for Android using Android Studio (on Ubuntu). The way I link my library to my app is as follows:

    settings.gradle:

    rootProject.name='MyApp'
    include ':app', ':codifier-promise'
    project(':codifier-promise').projectDir = new File("../Library/Promise/promise")
    

    build.gradle (Module: app):

    ...
    
    dependencies {
        ...
        implementation project(':codifier-promise')
    }
    
    

    In other words, I link to the library directory, in stead of hard copying the code into my app. I like this approach, because, in principle, it allows me to always have the link to the latest version, in stead of having to copy it in each time I changed something.

    However, as a consequence of this, when I (temporarily) check out another Git branch of my library (that I don't see fit for production yet), this automatically also is the library "version" that will show up in my app's code.

    My aim is to only link to a particular "version" of my library, that I am happy with. Preferably without having to link to an external server like Github, etc., even though my Promise repository is currently hosted on Github as well.

    Because, in the meanwhile I've actually found a way to achieve what I want with code hosted on Github. But, I'd just like to know if I can achieve something similar locally as well, for when I decide to include a local library that I don't want to host on an external server.

6
  • “this change is immediately reflected in MyApps code as well” please tell us what’s making that happen by editing your question. Commented Jul 3, 2020 at 0:50
  • As long as there's no collaboration, maven is far from "advanced". You just use mvn install to install the specific version in your local cache, and then use that version in your app.
    – mtj
    Commented Jul 3, 2020 at 4:31
  • 1
    @candied_orange I've edited my question, to address your question. Please let me know if this clarifies it enough for you. Thanks.
    – Codifier
    Commented Jul 3, 2020 at 5:16
  • @mtj It's not? Perhaps I've misunderstood Maven then. My understanding was that I'd have to prepare my library for export to some centralized Maven repository (much like Github) and then maybe install some tool and learn additional configuration settings again. I currently just want to avoid having to learn and dig into all the configuration options of yet another tool for developing, if possible. But your comment suggests that you can use Maven locally as well (without depending on an external server), is that correct?
    – Codifier
    Commented Jul 3, 2020 at 5:27
  • @Codifier Yes.You only have to go through all the public-repository-hassle if you want to make your library public. For local development, just give the library a non-snapshot version number, mvn install and use it. (And then go back to a snapshot version, so that you don't overwrite that release by accident.) The only thing to watch out for is, that the version will disappear if you ever delete your local repository cache or move to a different machine.
    – mtj
    Commented Jul 3, 2020 at 6:02

1 Answer 1

1

Is there a convenient way to only include a snapshot of the library's Git code, without having to make a copy of the library?

Git submodules.

A submodule holds a reference to a different Git repository. As per the docs:

A submodule is a repository embedded inside another repository. The submodule has its own history; the repository it is embedded in is called a superproject. [T]he superproject tracks the submodule via a gitlink entry [...] The gitlink entry contains the object name of the commit that the superproject expects the submodule’s working directory to be at.

The documentation even describes your exact use case:

Using another project while maintaining independent history. Submodules allow you to contain the working tree of another project within your own working tree while keeping the history of both projects separate. Also, since submodules are fixed to an arbitrary version, the other project can be independently developed without affecting the superproject, allowing the superproject project to fix itself to new versions only when desired.

Git submodules allow you to keep the projects seperate. You can develop the library in a bubble, and when the App requires an updated version, you can check out and pin the latest version in your submodule.

1
  • Okay, this looks really promising. I will take some time to investigate and try it out (particularly trying to incorporate it in a Android Studio project), but I'll come back to it. Thank you very much, so far.
    – Codifier
    Commented Jul 3, 2020 at 12:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.