2

I am working in a project which have few layers, each of them independent sub-projects.

It looks like this:

[ Project C ] [ Project D ]  C and D uses B and therefore A
[ Project B ]                B has all source code in a and adds more
[ Project A ]                

Setting upstream to the parent repository and issuing git pull upstream works well to get the changes from the upper project.

However, how would I push a commit in the scenario of bug fix in C which affects the code shared with the parent layers?

If I issue git pull project-D from Project A, it will also merge all the source code added at that layer.

I also considered using sub-trees or submodules, but it still looks complicated.

msw
  • 42,753
  • 9
  • 87
  • 112
Vicente Bolea
  • 1,409
  • 16
  • 39

1 Answers1

2

* I also considered using sub-trees or submodules, but it still looks complicated.*

This is exactly what you need to use. and let me explain how its pretty simple and not so complicated

Submodules allow foreign repositories to be embedded within a dedicated subdirectory of the source tree, always pointed at a particular commit.


git submodule

Break your big project to sub projects as you did so far.
Now add each sub project to you main project using :

git submodule add <url>

Once the projected is added tot your repo you have to init and update it.

git submodule init
git submodule update

As of Git 1.8.2 new option --remote was added

git submodule update --remote --merge

will fetch the latest changes from upstream in each submodule, merge them in, and check out the latest revision of the submodule.

As the docs describe it:

--remote

This option is only valid for the update command. Instead of using the superproject’s recorded SHA-1 to update the submodule, use the status of the submodule’s remote-tracking branch.

This is equivalent to running git pull in each submodule.


However, how would I push a commit in the scenario of bug fix in C which affects the code shared with the parent layers?

Again: using submodule will place your code inside your main project as part of its content. The difference between having it locally inside the folder or having it as part of a submodule is that in submodule the content is managed (commited) to a different standalone repository.


This is an illustration of submodule - project inside another project in which each project is a standalone project.

enter image description here


git subtree

Git subtree allows you to insert any repository as a sub-directory of another one

Very similar to submodule but the main difference is where your code is managed. In submodules the content is placed inside a separate repo and is managed there which allow you to clone it to many other repos as well.

subtree is managing the content as part of the root project and not in a separate project.

Instead of writing down how to set it up and to understand how to use it you can simply read this excellent post which will explain it all.

https://developer.atlassian.com/blog/2015/05/the-power-of-git-subtree/

CodeWizard
  • 128,036
  • 21
  • 144
  • 167
  • That is a very detailed answer, actually I got a better understanding of how does it works. @codeWizard, I wonder if there is anywhere in the github any Autotools C++ projects which follows that architecture. As far as I know autotools allows to have an 'umbrella type' of project but it might be quite complex to make it happens without duplicating any part of the code, even Makefile.am or configure.ac. I know it's off topic bringing up Autotools to this post, but I wonder if you have any thought about it – Vicente Bolea Mar 14 '16 at 08:33
  • 1
    Here is a sample project: https://github.com/nvie/gitflow `shFlags` is a submodule. Another example with c++ code: https://github.com/mfontanini/libtins but submodule has nothing to do with the code language you ar using. – CodeWizard Mar 14 '16 at 10:03
  • I have no experience with autotools but ill be happy to help with any Git issue you have :-) – CodeWizard Mar 14 '16 at 10:03