10

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.

svick
  • 236,525
  • 50
  • 385
  • 514
pek
  • 17,847
  • 28
  • 86
  • 99

2 Answers2

10

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.

Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199
  • I did not notice that it has a separate config file. That made everything clearer. – pek Jun 17 '12 at 16:09
2
git remote add upstream git://github.com/otherUser/otherProject.git

will only work if the git protocol (by default, port 9478) is aithorized.
I couldn't do it from work (and I don't even mention ssh addresses: ssh is also blocked from work from "outside-the-LAN" remote addresses).

git remote add upstream https://github.com/otheruser/otherProject.git

will work (providing some additional settings)

Note: creating an upstream in your submodule repo (as CodeGnome described, upvoted) is useful for you to update that submodule with the latest ofotherProject

cd myForkOfOtherProject
git fetch upstream
# or fetch + merge:
git pull upstream

It won't be used for you to push back to otherProject (if you forked it, it is probably because you are not listed as collaborator).
If you make any evolution directly in myForkOfOtherProject, you will need to commit it, then go to the parent repo and commit it as well (see "true nature of submodules"). And make a pull request if you wish for the initial otherProject maintainer to take that into account.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250