Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to link folder from a git repo to another repo?

Tags:

git

github

gitlab

I want to create a public repo to put some sample files from my main repo (private). Is there any way to soft link few folders from a git repo to another git repo?

like image 292
Manoj Avatar asked Apr 11 '16 16:04

Manoj


People also ask

How do I link a Git repo to another repo?

git submodule Once the project is added to your repo, you have to init and update it. will fetch the latest changes from upstream in each submodule, merge them in , and check out the latest revision of the submodule.

How do I link an issue to a different repo?

You can link to a commit or issue in a different repository by writing out its full GitHub URL. GitHub will shorten it appropriately when displaying it. This works throughout the GitHub UI, including commit messages.

How to soft link few folders from one Git repo to another?

Is there any way to soft link few folders from a git repo to another git repo? Then you should use submodules for this task. Submodule are different git repositories under the same root. Submodules allow foreign repositories to be embedded within a dedicated subdirectory of the source tree, always pointed at a particular commit.

How to link local repository to remote repository in Git?

There are always a few ways to link the local repository to the remote repository. From remote: This happens with the help of the Git Fork command when the repository is already available on GitHub. User makes a git fork or git clone to the same repository at local. (Note: We cover this in the next chapter)

How do I clone a subfolder in a git repository?

Clone the repository that contains the subfolder. Change the current working directory to your cloned repository. To filter out the subfolder from the rest of the files in the repository, run git filter-repo, supplying this information: FOLDER-NAME: The folder within your project where you'd like to create a separate repository.

How to move content from one Git repo to another?

git remote -v ​ # or peek directly into .git subdirectory ls .git/logs/refs/remotes The reset command plays a critical role as you move content from one git repo. It discards what you no longer need, creating a space for incoming content. It exists in three forms: mixed, soft, and hard.


1 Answers

Then you should use submodules for this task.

Submodule are different git repositories under the same root.
This way you can manage 2 different project at folder level inside the root repository

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 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/

like image 140
CodeWizard Avatar answered Sep 28 '22 18:09

CodeWizard