Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swap git submodule with own fork

I added a submodule to my git repo like this:

$ git submodule add git://github.com/user/some-library some-library

I've decided I want to create a fork of that library to do some adjustments. How can i swap that submodule so that it points to my own github fork instead?

like image 916
Svish Avatar asked Jul 24 '12 18:07

Svish


People also ask

Does git clone pull submodules?

It automatically pulls in the submodule data assuming you have already added the submodules to the parent project. Note that --recurse-submodules and --recursive are equivalent aliases.

Is using git submodules a good idea?

Git submodules may look powerful or cool upfront, but for all the reasons above it is a bad idea to share code using submodules, especially when the code changes frequently. It will be much worse when you have more and more developers working on the same repos.


2 Answers

The submodules are stored in .gitmodules:

$ cat .gitmodules
[submodule "ext/google-maps"]
    path = ext/google-maps
    url = git://git.naquadah.org/google-maps.git

If you edit the url with a text editor, you need to run the following:

$ git submodule sync

This updates .git/config which contains a copy of this submodule list (you could also just edit the relevant [submodule] section of .git/config manually)

There might be a way to do it with only git commands, although the submodule system seems a bit incomplete (e.g see the instructions to remove a submodule)

like image 183
dbr Avatar answered Oct 16 '22 21:10

dbr


I don’t know how long it is true, but certainly even in rather old git 1.8 submodules have remotes. So, you can do

cd some-library   # this is entering the submodule
git remote -v     # just to see what you have; I guess origin only
git remote add myrepo git-URL-of-the-fork
git checkout -b my_new_idea
git push -u myrepo my_new_idea

Now the submodule work with your fork instead of the original repo. You can do normal push, pull, rebase etc. When your pull request is merged, you can remove the remote with normal git remote remove myrepo. Of course, all other caveats about working with submodules apply (e.g., you have to commit the change of the submodule as a new commit in the supermodule).

like image 8
mcepl Avatar answered Oct 16 '22 23:10

mcepl