1

I want to push the same code from my local using git + vscode to two github repos with only one command without individually specifying each remote.

I have tried adding 2 remote repos in the config file: Updated config file:

cat config
[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
        ignorecase = true
        precomposeunicode = true
[remote "origin"]
        url = git@github.com:profile1/-MS_E-commerce_Grus_Tep_Online_Full-time_2021_1.git
        fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
        remote = origin
        merge = refs/heads/master
[remote "github"]
        fetch = +refs/heads/*:refs/remotes/github/*
        url = git@github.com:profile2/e-commerceAPI.git
        pushurl = git@github.com:profile2/e-commerceAPI.git

Now when I do git push it only pushes to one repo, this one

profile1/-MS_E-commerce_Grus_Tep_Online_Full-time_2021_1.git

I have tried using git push -u github main but it throws an error:

error: src refspec main does not match any, error: failed to push some refs to 'github.com:profile2/e-comm..

I have tried with git push -u origin main. But, I'm completely unfamiliar with the process and this is my 1st attempt. Please suggest. I could also go for a way to push it with multiple steps every time as long as my commits are preserved.

Thank You

Anupam Arya
  • 169
  • 1
  • 1
  • 12

1 Answers1

1

The git push command only pushes to one (1) repository, each time you run it. You pick the (as in, one) repository that it should push to, at the time you run it, using the argument origin or github, given your two remotes:

git push origin

or:

git push github

The former connects to:

url = git@github.com:profile1/-MS_E-commerce_Grus_Tep_Online_Full-time_2021_1.git

and the latter connects to:

pushurl = git@github.com:profile2/e-commerceAPI.git

It uses the pushurl setting since it has a pushurl setting; as origin lacks such a setting, it uses the url setting instead.

In either case, having made this connection, the set of commits and other objects that your git push will send, and the set of requests it will make to whoever answers at the URL, will depend on the last argument. So if you want to send your master branch commits each time, and have the GitHub repository update its master branch each time, you would use master each time:

git push origin master
git push github master

You should run two commands to update two remotes. This allows you to detect which one failed, if one fails.

I want to push the same code from my local using git + vscode to two github repos with only one command without individually specifying each remote.

You can't do that. It turns out that git push will do this, but it's not documented. As such, it could potentially stop working in the future. It's certainly safer to do something else: You can, for instance, set up an alias or script that contains the two commands, and run the alias or script, at least from the command line. Whether, and how, you can do this from the mouse in VS, I have no idea.

To do this with Git, simply set two or more urls or pushurls under any one given remote, e.g.:

[remote "pushpair"]
    pushurl = git@github.com:profile1/-MS_E-commerce_Grus_Tep_Online_Full-time_2021_1.git
    pushurl = git@github.com:profile2/e-commerceAPI.git

Running git push pushpair master will now try to push to each of the two pushurl settings in sequence. Either can fail; Git will still try the remaining one if one fails. This is based on the Git source code, not the documentation.

(Note that if you use one pushurl, any url settings are ignored, so both must be a pushurl. If you use url without pushurl, and set multiple URLs, all will be used. However, it's not clear how git fetch will behave here.)

torek
  • 448,244
  • 59
  • 642
  • 775
  • OP> "*I want to push the same code from my local using git + vscode to two github repos with only one command without individually specifying each remote.*" torek> "*You can't do that.*? `git remote set-url origin --push --add`, no? Like in https://stackoverflow.com/a/3195446/7976758 – phd Feb 07 '22 at 13:30
  • @phd: Interesting. It's not documented, and I wouldn't trust it even if I'd tested it and found it worked. However, checking the source code, it will actually work. The lack of documentation is worrisome. I'll update the answer. – torek Feb 07 '22 at 13:52