简体   繁体   中英

Git: Push to multiple remotes

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM