3

Short: If you have linked more than one remote repository to your local files, is there a way to push to both of them?

Long: Due to work reasons, I have to use gitlab and github remotes for my code. So as soon as I make some local changes I would like to update both repositories.

My .git/config file looks like this

[core]
  repositoryformatversion = 0
  filemode = true
  bare = false
  logallrefupdates = true
  ignorecase = true
  precomposeunicode = true
[remote "origin"]
  url = https://github.com/myusername/myproject.git
  fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
  remote = origin
  merge = refs/heads/master
[remote "gitlab"]
  url = https://gitlab.com/myusername/myproject.git
  fetch = +refs/heads/*:refs/remotes/gitlab/*

As far as I understand the git status command works only with the origin remote, is this true? Push and pull does work with my origin remote as well, unfortunately, I cannot add stuff to my other remote (gitlab) with git add somefile.txt gitlab

How do i push stuff to my gitlab remote?

nieka
  • 259
  • 1
  • 4
  • 14
  • 1
    Possible duplicate of [pull/push from multiple remote locations](https://stackoverflow.com/questions/849308/pull-push-from-multiple-remote-locations) – StarShine Nov 08 '17 at 10:21
  • You add files to commits, and push commits to remotes. You never add files to remotes. – Nils Werner Nov 08 '17 at 10:22
  • 2
    Maybe it makes more sense to [mirror the repositories](https://docs.gitlab.com/ee/workflow/repository_mirroring.html#setting-up-a-mirror-from-gitlab-to-github) rather than explicitly pushing to both? – Matthew Daly Nov 08 '17 at 10:25
  • https://stackoverflow.com/questions/14290113/git-pushing-code-to-two-remotes I think this link will help you. – Kshitij Kythe Nov 08 '17 at 10:25
  • @MatthewDaly That looks like a good option here; personally I have never needed to publish to more than one repo. – Tim Biegeleisen Nov 08 '17 at 10:27

1 Answers1

5

If you need to push a local branch to both remotes, then do so via:

git push origin master
git push gitlab master
           ^^^ specify the remote to be used here

I don't think in general that pushing to two remotes at the same time is desirable. What should happen if one push succeeds and the other fails? This is not clear.

Note that most Git commands are atomic, meaning that they happen to completion, or they do not happen at all. This means that we don't need to worry about about a git push ending up in a partially complete, broken state. Rather, a push either happens as you specified, or it did not happen at all.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • 1
    Re. "I don't think there is a single command to push to multiple remotes" ... reading the accepted answer to https://stackoverflow.com/questions/14290113/git-pushing-code-to-two-remotes sounds like you can push to multiple remotes with one command ? – SteveC Mar 05 '19 at 08:27