This article aims to explain the need and use of git subdir strategy during work with different projects in context with Android

While working on a project I encountered a strange situation once and that was that I had to import another project inside of my current project. The situation was strange because I had to keep another project that I was going to import updated or will have to merge the latest updates from time to time.

Example Situation

A nice explanation of the situation would be something like this

  • My current project: An Android Kernel
  • Target project to import: QCOM’s WLAN module
  • Aim: To ship a standalone kernel with a different OS with a working wifi

I am required not only to import the current project but also would be needed to do device specific changes to the WLAN module and enable its compilation with my Kernel.

Note:

In case, if I just want to import or track another project without doing any changes, I could have used git submodule feature which lets you import another repo/project in your current project without any additional work.

However, it’s important to note that git submodule will let me track that another project into my current repo but it the disadvantage will be that I would have to merge my changes in target repo which I am going to track and as the changes are device specific, it makes even less sense to merge my changes into a project which is generic rather than device specific.

I can obviously fork my copy of target project and track it, but lets for the sake of this article we don’t assume that option here.

Enter git subtree stratergy

Git-subtree strategy as the name suggests, allows you to import another project into a specific directory as well as lets you merge, cherry-pick and do all the cool stuff that you would normally do in the root of your project.

How to use the git subtree strategy?

I will use an example situation to explain it. Proper commands regarding what you want to do with subtree strategy can be obtained using man git or the web.

  • My Current Project: An Android kernel
  • Target Project to Import: prima repo of CodeAurora
  • Aim: Import prima repo to a subdir of current kernel project to do tinker around

Add the target project as remote and import it.

git remote add caf url_of_repo

git subtree add --prefix=path/to/dir remote_name branch_name

Example

git remote add prima https://source.codeaurora.org/quic/la/platform/vendor/qcom-opensource/wlan/prima

git subtree add --prefix=drivers/staging/prima prima LA.BR.1.2.9.1-02310-8x16.0

You can see that I fetched a certain tag. However, importing a branch is also possible. You can take a look at documentation by using man git or search the web, ask on forums, read books and all.

Credits: