You can add multiple remotes to a Git repository. This is useful if, for example, you want to push code to both GitHub and GitLab, or collaborate with multiple forks.
Add a second remote:
git remote add <remote-name> <remote-url>
Example:
Suppose your main remote is origin
(default), and you want to add a second one called backup
:
git remote add backup https://gitlab.com/yourusername/yourrepo.git
Check remotes:
git remote -v
Push to specific remote:
git push origin main # push to GitHub
git push backup main # push to GitLab
Fetch from all remotes:
git fetch --all
You can add as many remotes as needed — just give each one a unique name.
You can have multiple URLs for the same remote name (like origin
) in Git — but with some limitations and use cases:
Use Case 1: Push to multiple URLs at once
You can set multiple push URLs for a single remote. This is helpful when you want to push to several repositories simultaneously (e.g., GitHub and GitLab).
Example:
git remote set-url --add --push origin https://github.com/youruser/yourrepo.git
git remote set-url --add --push origin https://gitlab.com/youruser/yourrepo.git
Now when you do:
git push origin main
It will push to both URLs.
But: For fetching, Git supports only one fetch URL per remote.
If you try:
git remote set-url --add origin https://github.com/youruser/yourrepo.git
git remote set-url --add origin https://gitlab.com/youruser/yourrepo.git
…then Git will use only the last one for fetches.
To check what’s set:
git remote show origin
Recommended pattern:
- Use multiple push URLs for
origin
- Use separate remotes (
origin
,gitlab
, etc.) if you also want to fetch from multiple places
Note: If you’re using Git LFS in this setup where there are multiple push urls for a single remote, you will have to explicitly add the large files to each of the push urls like git lfs push --all https://gitlab.com/youruser/repo.git
.