Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: Push to multiple remotes

Tags:

git

github

gitlab

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?

like image 922
nieka Avatar asked Dec 05 '25 07:12

nieka


1 Answers

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.

like image 140
Tim Biegeleisen Avatar answered Dec 07 '25 20:12

Tim Biegeleisen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!