7

I'm new to Git version control. I just want each project to have its own repository.

I created

  1. A Project(Shared Lib) in RepoA
  2. B Project(Shared Lib) in RepoB that has subtree of A
  3. C Project(Shared Lib) in RepoC that has subtree of A
  4. D Project(Console Application) in RepoD that has subtree of B and C

the folder structure is like below.

D

-B

--A

-C

--A

As you can see, A folder is duplicated, and I can't add the same project twice in visual studio.

If I add A project in B folder, the compile error occurs.

Metadata file project C..... 'A.dll' could not be found Because A project in C folder, never been compile, A.dll is missing.

If A in C project is compiled once, it works fine.(by opening Either C.sln or A.sln in C project) Of course, there would be A.dll,

but next time someone else download D project from git, he/she should compile C or A in C project before compile D project.

Anyone has a good solution for this?

johnnyRose
  • 7,310
  • 17
  • 40
  • 61
Han Eui-Jun
  • 142
  • 1
  • 8

1 Answers1

6

You can try and read about submodules.

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/

Community
  • 1
  • 1
CodeWizard
  • 128,036
  • 21
  • 144
  • 167
  • Thanks for the answer. but I want to use subtree. because of below reasons. 1. I need to update libraries while I develop console application. If I use submodule, I need to open the library in another solution, changed code, commit, push then I have to update the module from my console solution. This is what I understand about submodule. 2. Even with submodule, It does not solve the above problem. Thanks. – Han Eui-Jun Mar 30 '16 at 14:59
  • No problem, that why i gave you explanation about the options you can use. – CodeWizard Mar 30 '16 at 15:53
  • 1
    Actually your answer helped me to understand subtree more deeply, however, it's not the answer I wanted, but there is no any other answer. I will accept yours as the answer. Thanks ;) – Han Eui-Jun Apr 01 '16 at 15:52
  • I had the same question and thought it was a good answer. I came to the same conclusion - that for VS 2017 subtree is "better", but it looks more like an artifact of how the VS git plugin works, and doesn't necessarily represent the ideal approach, so I like understanding the difference between the submodules and subtrees. – Robear Sep 02 '17 at 17:25