[{"data":1,"prerenderedAt":20},["ShallowReactive",2],{"page:\u002Fresource-library\u002Fmodular-repositories-with-git":3},{"path":4,"layout":5,"title":6,"description":7,"ogImage":8,"post":9,"html":19},"\u002Fresource-library\u002Fmodular-repositories-with-git","post","Modular repositories with Git","Git submodules vs. subtrees: two ways to build modular repositories, how each one works, and the tradeoffs for sharing code between projects.","\u002Fimages\u002Fresource-library\u002Fmodular-repositories-with-git\u002Fgit-logo-render-social.jpg",{"author":10,"published":11,"updated":12,"tags":13,"image":17,"imageAlt":18},"Krithik Chandrashekar","2017-02-23",null,[14],{"name":15,"slug":16},"Embedded systems and software","embedded-software","\u002Fimages\u002Fresource-library\u002Fmodular-repositories-with-git\u002Fgit-logo-render.webp","3D render of the Git logo, a red branching graph on a black diamond","\u003Ch2>Submodules\u003C\u002Fh2>\n\u003Cp>Modular programming is a design technique that has proven to be a generally beneficial approach\n  for software development. It leads to code that is easier to extend, reuse and debug. To take the\n  approach to the next level would be incorporating entire projects as modules under another\n  project. This scenario becomes commonplace when including open source libraries into your\n  projects.\u003C\u002Fp>\n\u003Cp>This strategy generally works best when integrated with formal version control systems like Git\n  or SVN. Depending on how this is set up with your preferred choice of version control, it could\n  leave you with a reasonably maintainable structure to work with or an unsightly mess that is\n  difficult to work with. This is especially true if you do not clearly understand the shortcomings\n  of a particular approach or are not even aware of an alternative.\u003C\u002Fp>\n\u003Cp>With Git specifically, the default and most popular approach seems to be going with the \u003Ca\n  href=\"https:\u002F\u002Fgit-scm.com\u002Fdocs\u002Fgit-submodule\" target=\"_blank\" rel=\"noopener\">submodule\u003C\u002Fa> route.\n  The setup is straightforward. Say, for instance, you want to include a submodule within an\n  existing Git repository.\u003C\u002Fp>\n\u003Cpre>\u003Ccode>$ git submodule add &lt;submodule_source_remote_url_here&gt;\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cp>This will add a .gitmodules file with details about the submodule and an empty directory which\n  takes the name of the Git repository. When cloning a fresh copy of this repository, you will need\n  to add the \u003Ccode>--recursive\u003C\u002Fcode> option to bring in the files for the submodule.\u003C\u002Fp>\n\u003Cpre>\u003Ccode>$ git clone --recursive\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cp>When changes to the submodule are applied independently on the submodule repo itself, changes can\n  be pulled in.\u003C\u002Fp>\n\u003Cpre>\u003Ccode>$ git submodule update --recursive\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cp>One of the drawbacks when including external open-source projects as submodules is\n  \u003Cstrong>changes\u003C\u002Fstrong> made to the files in the submodule folder will \u003Cstrong>remain\n  local\u003C\u002Fstrong> and cannot be propagated directly to the remote source repository for which you may\n  not have permissions.\u003C\u002Fp>\n\u003Cp>For this reason, it might be beneficial to host a local fork of the source repository in your own\n  flavor of Git host (GitHub, Bitbucket, GitLab, etc.) for which you have permissions to make\n  changes and then propagate them down.\u003C\u002Fp>\n\u003Cpre>\u003Ccode>$ cd &lt;submodule_folder&gt;\n$ git add .\n$ git commit -m \"write something meaningful\"\n$ git push\n$ cd ..\u002F\n$ git add &lt;submodule_folder&gt;\n$ git push\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Ch2>Subtrees\u003C\u002Fh2>\n\u003Cp>Another alternative that is less commonly used is \u003Ca\n  href=\"https:\u002F\u002Fgit-scm.com\u002Fbook\u002Fen\u002Fv1\u002FGit-Tools-Subtree-Merging\" target=\"_blank\"\n  rel=\"noopener\">subtrees\u003C\u002Fa>. Working with subtrees will add a few more steps to the process\n  compared to submodules but might be the better solution to this problem. With subtrees, you no\n  longer just maintain a reference file to the source files of your module, but you include all the\n  files of the modules instead. Managing subtrees comes down to just managing \u003Ca\n  href=\"https:\u002F\u002Fgit-scm.com\u002Fdocs\u002Fgit-remote.html\" target=\"_blank\" rel=\"noopener\">remotes\u003C\u002Fa>.\u003C\u002Fp>\n\u003Ch3>In your main project\u003C\u002Fh3>\n\u003Cul>\n\u003Cli>\u003Cstrong>Main Project Remote\u003C\u002Fstrong>: The remote for the project that you are working on that\n  will use the module\u003C\u002Fli>\n\u003Cli>\u003Cstrong>(Module) Subtree Project Remote\u003C\u002Fstrong>: This will point to the locally hosted fork of\n  the original repository\u003C\u002Fli>\u003C\u002Ful>\n\u003Cp>The first remote should automatically be present if you have cloned the repository from the\n  remote URL. It needs to be added explicitly if you have initialized a non-Git folder as a Git\n  project. The second remote will need to be added explicitly.\u003C\u002Fp>\n\u003Cpre>\u003Ccode>$ git clone &lt;url_main_project&gt;\n$ git remote add -f &lt;name_module_remote&gt; &lt;url_module_remote&gt;\n$ git subtree add --prefix &lt;submodule_folder_name&gt; &lt;submodule-remote-name&gt; master --squash\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cp>This should bring in all files from the module repository. The \u003Ccode>'--squash'\u003C\u002Fcode> argument\n  is necessary to prevent history from the subtree getting mangled to the main repository's Git\n  history.\u003C\u002Fp>\n\u003Ch3>In your module project\u003C\u002Fh3>\n\u003Cul>\n\u003Cli>\u003Cstrong>(Module) Project Remote\u003C\u002Fstrong>: This will point to the locally hosted fork of the\n  original repository\u003C\u002Fli>\n\u003Cli>\u003Cstrong>(Module) Project Source Remote\u003C\u002Fstrong>: This will point to the source repository from\n  whence it was forked\u003C\u002Fli>\u003C\u002Ful>\n\u003Cp>The second remote for the module project is optional and exists only if you are using an external\n  project as a module.\u003C\u002Fp>\n\u003Cp>This way, you can pull upstream changes from the source to the \u003Cstrong>fork\u003C\u002Fstrong> first and\n  then have the option to propagate the same changes to any projects that make use of this\n  module.\u003C\u002Fp>\n\u003Cpre>\u003Ccode>$ git remote add -f &lt;source-remote-name&gt; &lt;source-url&gt;\n$ git pull &lt;source-remote-name&gt; &lt;working-branch&gt;\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Ch3>Pulling changes from module to main project\u003C\u002Fh3>\n\u003Cp>If you have made commits to your module project and want to bring those changes into your main\n  project:\u003C\u002Fp>\n\u003Cpre>\u003Ccode>$ git subtree pull --prefix=&lt;submodule_folder_name&gt; &lt;submodule_remote_name&gt; master --squash\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Ch3>Pushing changes from main project to module project\u003C\u002Fh3>\n\u003Cpre>\u003Ccode>$ git subtree push --prefix=&lt;submodule_folder_name&gt; &lt;submodule_remote_name&gt; master\u003C\u002Fcode>\u003C\u002Fpre>",1795194794062]