Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting upstream to a submodule (or how to include a GitHub fork as a submodule)

I'm quite new to this Git thing and need a little help.

I recently created a new repo on GitHub and cloned it on my desktop. Let's call it myProject. I also have a fork in my GitHub account which I included in myProject as a submodule. Let's call this myForkOfOtherProject, which is a fork of otherProject.

So, this is the current situation:

Graph of the current state of repos

According to GitHub:

When a repo is cloned, it has a default remote called origin that points to your fork on GitHub, not the original repo it was forked from. To keep track of the original repo, you need to add another remote named upstream.

So, my question is, how do I set the upstream to the submodule? Or am I not understanding something?

Thank you in advance.

like image 428
pek Avatar asked Jun 17 '12 07:06

pek


People also ask

How do I add an existing git repository to a submodule?

In order to add a Git submodule, use the “git submodule add” command and specify the URL of the Git remote repository to be included as a submodule. When adding a Git submodule, your submodule will be staged. As a consequence, you will need to commit your submodule by using the “git commit” command.

How do I pull from upstream fork?

Go to your fork, click on Fetch upstream , and then click on Fetch and merge to directly sync your fork with its parent repo. You may also click on the Compare button to compare the changes before merging.

Can submodules have submodules?

Cloning a Project with Submodules If you pass --recurse-submodules to the git clone command, it will automatically initialize and update each submodule in the repository, including nested submodules if any of the submodules in the repository have submodules themselves.


1 Answers

Submodules

When you clone a submodule into your repository, it will have its own .git/config file and its own notion of origin. Assuming that the submodule is yours (e.g. there is no third-party repository upstream of your remote) then you don't need to worry about creating an upstream remote for the submodule.

If you do need to create an upstream remote for your submodule, it's easy enough. Just cd into the top-level directory of your submodule, and give it one the same way you did for the main repository.

cd myForkOfOtherProject
git remote add upstream git://example.com/otherProject.git

There is no namespacing conflict, because a submodule is really just a normal git repository with some additional meta-information tracked in the superproject. The superproject and the submodule do not share their .git/config files.

For all intents and purposes, you handle origin and upstream inside the submodule the same way you do for any other repository. Git commands that you run inside the submodule are independent of the superproject, which is mostly interested in tracking the submodule's current commit ID.

like image 119
Todd A. Jacobs Avatar answered Oct 22 '22 22:10

Todd A. Jacobs