47

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?

Giovanni Cerretani
  • 1,693
  • 1
  • 16
  • 30
Svish
  • 152,914
  • 173
  • 462
  • 620
  • 1
    This may be relevant to some people - you can't make a private fork of a public repository, so if you want your copy of the repo to be private you need to [duplicate the repository](https://stackoverflow.com/questions/10065526/github-how-to-make-a-fork-of-public-repository-private/54139120). – yoyo Sep 29 '20 at 18:52

2 Answers2

65

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)

Community
  • 1
  • 1
dbr
  • 165,801
  • 69
  • 278
  • 343
  • 7
    `git submodule sync` command can be used to synchronize .git/config with changes in .gitmodules – lisachenko Jul 24 '12 at 19:36
  • 5
    Just wanted to point out something that might not be self-evident: I wanted to use my fork because I had made some commits I wanted. Updating the submodule URL and syncing does not include any new commits, it only checks out the same commit from the new fork. To get the latest revision, you still need to cd into the submodule, checkout master (or whatever), and then commit the new version of the submodule in the parent repo. –  Oct 30 '15 at 16:45
  • 1
    update after ten years: there's a new command, `git submodule set-url`, see https://stackoverflow.com/questions/65796573/git-github-replace-a-submodule-of-a-forked-repository – koddo Mar 02 '23 at 07:39
9

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).

mcepl
  • 2,688
  • 1
  • 23
  • 38
  • Note that pull / fetch take a repository parameter, which defaults to "origin". So you'll need to `git fetch myrepo` before the branches of the new fork will show up in your local workspace. – yoyo Sep 29 '20 at 17:15
  • I think this is the cleaner approach as compared to editing `.gitmodules` manually. – Martin Hepp Nov 11 '21 at 15:35
  • @MartinHepp, if I understand it correctly, this approach is easier when you work alone, but if you have to share the fork with others, this probably won't work, unless you tell everyone to add your remote. I haven't tried this, but modifying `.gitmodules` should ensure everyone now uses the fork. I hope so, because otherwise this would require everyone to re-init this submodule, which would be effectively the same thing as adding a new remote. – koddo Mar 02 '23 at 07:26