Maintaining a Git repo inside another git repo
Asked Answered
S

3

25

I have a git repo which contains an AngularJS web app.

It has a subfolder called build, which gets created by a gulp task. I am deploying to Azure, so it is connected directly to my bitbucket directory.

I want to make build folder as a separate git repo from which the Azure app is being deployed. How do I achieve this in git??

Submergible answered 26/3, 2016 at 14:30 Comment(2)
I have a sneaking suspicion there is some over engineering going on here, but you should look into git submodulesDauphine
Possible duplicate of Maintain git repo inside another git repoManservant
R
42

You have several options like:

  • submodule
  • subtree

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 into sub-projects as you did so far.
Now add each sub-project to you main project using:

git submodule add <url>

Once the project is added to 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/

Ransell answered 27/3, 2016 at 18:15 Comment(2)
Is there any other solution without submodul and subtree, as describe in #47008790 ?Candler
Good additional context for subtree in this question.Crusted
C
13

You can do this with git submodule or subtree, I use submodule for this kind of reason.

https://git-scm.com/docs/git-submodule

example :

/mainrepository
/mainrepository/subrepository

cd /mainrepository/subrepository;
git init .
cd ../
git submodule add ./subrepository

then open seperate remote repository in bit bucket then 
cd into ./subrepository
git remote add origin https://bitbucket.com/path/to/subrepository.git

basically it is all about that.

I have no detailed information about subtrees what I know, it is more advanced than submodules. But if your needs are basically matches with submodules it is easy to maintain.

Clyburn answered 26/3, 2016 at 14:43 Comment(2)
i want the branches of outer repo to point to same branches of inner repo......how will i achieve thisSubmergible
I think it is not possible, because main repo and submodule are 2 different repositories, so branching is based on repository, you cannot achieve like this. But main repo is tracking the submodule commit hashes when you add a repo as a submodule, so when you add any commit whatever branch in submodule you would see in main repo there would be last commit hash changed for submodule. So basically main branch also follows submodule. But it is not about branching. So why do you need that ? Is there any special approach.Clyburn
F
0

While the documentation on this is extensive [https://git-scm.com/book/en/v2/Git-Tools-Submodules], I found the solution was to understand how submodules work. This is a simplified plain english version.

  1. If you have your main repo, you have already initialised it with git ($ git innit) you may be getting an error if you have added another initialised repo as a submodule
  2. If you add a submodule which already has an initialised git repo you might want to remove git tracking ($ cd into the sub module then $rm -rf git) this force removes the files tracked by git - or before you add it to the repo remove the initialisation
  3. Check what exactly is going on with $ git diff / $ git diff --cached / $ git diff - - submodule if you have cached files the documentation runs you through what to do
  4. If you have a submodule that is not being tracked (which was my challenge) the documentation suggests creating a separate branch and merging this branch to master - this is what I did some of the steps which are here are not in the documentation as the documentation assumes a certain level of knowledge of git and it took me a while to figure out all the steps left out when I was starting learning git.
    $ git checkout -b stable (create a new branch called stable)
    $ git checkout stable (check into the new branch)
    $ cd .. (into your branch with the submodules)
    $ git submodule update --remote --merge (update and merge the submodule to the remote branch)
    $ git add . (add all files and directories  to the branch)
    $ git commit -m”adds submodule to new branch” (commit changes in the branch)
    $ git push (push changes in the branch) - this will remind you make the stable branch your upstream 
    $ git push --set-upstream origin stable (set upstream to your new branch)
    $ git checkout master (checkout into the master branch)
    $ git merge stable (merge pushed changes from branch to master)
    $ git add . 
    $ git commit -m”adds submodules from merged stable branch”
    $ git push origin master
  • Go into your online repo and check that you have the files.
  • If this is not the challenge you faced (adding an already initialised git module to a repo) it is a bit of a slog reading the documentation but worth it if you fix the challenge. Hope this helps anyone who has added an already initialised git submodule to a main git repo.
Fonseca answered 11/9, 2020 at 9:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.