git submodule update with other user

21,017

Solution 1

I assume that the submodule has already been initialized, so git config --list | grep ^submodule shows something like submodule.my-submodule.url=ssh://domain.com/abc/def.git.

If you haven't yet run git submodule update for the first time, then you can just change that config option, e.g. with:

git config submodule.my-submodule.url ssh://[email protected]/abc/def.git

On the other hand, if the submodule has already been updated once, then origin in the submodule will have been set to whatever that config option specified. In that case, you'll need to do:

cd my-submodule
git config remote.origin.url ssh://[email protected]/abc/def.git

It's just a bit confusing, I'm afraid, but submodules are very flexible. I made an attempt to explain some of these details in a blog post.

Solution 2

While the above solution works, I found a different solution that better suits my needs, and maybe the original requester's. I wanted a way to specify the default username for all git operations on a remote server instead of having to modify the git configs for each project. The solution doesn't really have anything to do with git at all, but ssh.

Simply add these lines to your ~/.ssh/config:

Host domain.com
User B

(replace domain.com with the domain of your git server.) Now, even if you are logged in to your local machine as user A, SSH will use B as the username when connecting to the server.

Now you can run git submodule update without having to add a username in the git config.

Solution 3

The short answer is 'check if you could use relative paths for submodules'

The detail is, We have submodule that is being used by multiple apps. We have kept the submodule in the same repo.

The structure is like this, repo

|-app1
|-app2
|-submod

When we clone the app the app/.git/config gets url with current user like '[email protected]' In the .gitmodules of apps we give url as '../submod' By this way, when we do 'submodule init', git generates absolute url for submodule from the relative url we gave in .gitmodules.

Share:
21,017

Related videos on Youtube

Mars
Author by

Mars

Python haxxorz and parapsychology student. Creator of the eminent finplot library: https://github.com/highfestiva/finplot. Once upon a time made a UFO short documentary in Swedish: https://www.youtube.com/watch?v=HFF528pc3O8. Constantly at a fork in the road.

Updated on July 09, 2022

Comments

  • Mars
    Mars almost 2 years

    I'm logged in as user A on my machine, but my repo is accessible through username B on the server that I pull from. The .gitmodules file has url = ssh://domain.com/abc/def.git.

    How can I configure git to use a username B instead of A when I do git submodule update?

    • Gi0rgi0s
      Gi0rgi0s about 5 years
      I'm wondering if there's a way to do this with a username/password pair rather than ssh.
    • Mars
      Mars about 5 years
      @Gi0rgi0s: if you like you could always add username and password to the ssh url (ssh://user:pass@host:port/path/to/src).
  • MaciekS
    MaciekS over 6 years
    That's a better solution, especially if you have a project with multiple submodules. Thanks!
  • yohann.martineau
    yohann.martineau almost 5 years
    as a reminder, ~/.ssh/config should not have write permissions on group, so chmod 644 should be ok for most cases.