Search code examples
gitgit-submodulesgit-remotesubtree

How to have editable git dependencies in my project (fe. Android)


I just wonder if somebody can help me solve this issue which Im trying to solve for several days...

I want to make private reusable libraries for my projects but I want to be able to edit these libraries (for example some library can provide me with prepared login screen and I want to tweak design of this screen) I already tried submodules, but that´s not really appropriate solution I also tried subtrees with multiple remotes but I think it will be messy when I have like 10 libraries in project (git tree will be huge and complicated)

In my head the behaviour should be something like this:

  • I add dependencies to my project
  • I edit and tweak these libraries
  • When I commit these changes, I want these changes be in my main git project, not in the library git
  • When I make some bugfixes in my libraries, I push these changes with TAG and then pull the changes to my main project and resolve conflicts if any show up

I discovered this plugin https://github.com/alexvasilkov/GradleGitDependenciesPlugin which seemed promising but have the same flaw as submodules (I must commit changes to the library git instead of main project git)

Is something like this even possible? Thanks for any help


Solution

  • Unfortunately, you cannot do what you describe in your question.

    Git works by taking snapshots of your whole project. Lets say this is your project:

    
    myproject
      |
      |\src
      |
      |\tmp
      |
      |\lib
      |  |
      |  |\lib1
      |  |
      |  |\lib2
    
    

    When you commit something in that project, git will take a snapshot of the whole code (all the modifications in all the files in all the folders inside myproject) and store it in your .git folder. This is a simplified description of what happens but for the sake of answering you is good enough.

    When you say:

    • I edit and tweak these libraries
    • When I commit these changes, I want these changes be in my main git project, not in the library git

    You cannot do it because the libraries are inside your project!! Git will take a snapshot of all your files when you commit, including those in the lib folder. Remember, git works at the snapshot level, not at the file level.

    As you said, you want to extract the libraries so you can reuse them in other projects. The only way to go is to extract those libraries to their own repository (probably one repository for each library) and include them in your projects as a dependency.

    Now, if you want to include those libraries as a dependency you can:

    • use git modules or git subtree as you already tried. As you said, can be a little cumbersome if you have a lot of things in your lib folder
    • create a package for each library and include it in your project using maven, npm, pip, bundler, nugget or whatever dependency management system your stack uses.