JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM > Git >

git remote operation——multiple remote repositories for one project

Author:JIYIK Last Updated:2025/04/05 Views:

Multiple remote repositories for a git project


In our git project, the command to operate the remote repository information is

$ git remote  # 查看当前所有的远程仓库的名称
$ git remote -v # 查看远程仓库的名称和远程仓库的网址

Generally speaking, when we clone the next project from the remote repository, the default remote repository name isorigin

$ git clone https://github.com/onmpw/JYGO.git
$ cd JYGO
$ git remote
origin

$ git remote -v
origin https://github.com/onmpw/JYGO.git (fetch)
origin https://github.com/onmpw/JYGO.git (push)

When pullor is used, the remote repository pushis used by defaultorigin

$ git pull origin master # 或者 git pull

$ git push origin master # 或者 git push

How do you add a remote repository to this project? Use git remote addthe command.

Method 1: git remote add


$ git remote add local git@localhost:workspace/repo/JYGO2.git

The above is the localsame originas the name of the remote warehouse

$ git remote -v
origin https://github.com/onmpw/JYGO.git (fetch)
origin https://github.com/onmpw/JYGO.git
local git@localhost:workspace/repo/JYGO2.git (fetch)
local git@localhost:workspace/repo/JYGO2.git (push)

git pushAfter adding successfully, you can push it to the required remote warehouse when you use it later . If you need to push it to two warehouses, execute both sidesgit push

$ git push origin master # 默认的origin远程仓库
$ git push local master  # 新添加的 local 远程仓库
To git@localhost:workspace/repo/JYGO2.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'git@localhost:workspace/repo/JYGO2.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

puahNo success! How could this happen? It turns out that after git remote addadding the remote repository using , you cannot push directly to the remote repository. That is to say, at the beginning, the second command above is not successful. Because at this time your local version localis inconsistent with the remote repository version, you need localto get the latest code from it. That is to say, pushyou need localto pull the latest content from it before executing .

$ git pull local master
Unpacking objects: 100% (3/3), done.
From git@localhost:workspace/repo/JYGO2.git
 * branch            master     -> FETCH_HEAD
 * [new branch]      master     -> github/master
fatal: refusing to merge unrelated histories

Yes, just using the above command will not work. You need to use the following command

$ git pull --allow-unrelated-histories local master

The execution was successful. Then pushyou can use it to push to the remote warehouse.

$ git push local master

Success. Great. But the problem comes with it. If there are multiple remote repositories that need to be submitted, then we have to execute it once for each remote repository git push. Is there a way pushto submit to multiple remote repositories at once? The answer is of course: there is a way!

Method two, git remote set-url --add Order


$ git remote set-url --add origin git@localhost:workspace/repo/JYGO2.git

The above command is to originadd a new remote warehouse address to the remote warehouse. There are many articles on the Internet that say that after adding, you can directly use git pushthe command to submit to multiple remote warehouses at once. In fact, it is not the case. The initial remote warehouse pushis successful. However, when it comes to the newly added address, the push fails.

$ git push --all # 或者 git push origin master 或者直接使用 git push, 都可以。
Enumerating objects: 8, done.
Counting objects: 100% (8/8), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (6/6), 931 bytes | 931.00 KiB/s, done.
Total 6 (delta 0), reused 0 (delta 0)
To https://github.com/onmpw/JYGO.git
   c1857d1..ff94cf0  master -> master
To git@localhost:workspace/repo/JYGO2.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'git@localhost:workspace/repo/JYGO2.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

Obviously, it is still because the local and remote versions are out of sync. That is to say, the content pushneeds to be uploaded before it can be downloaded pull.

$ git pull 
Already up to date.

It's very strange, it's already the latest content. But the newly added remote warehouse has not been obtained. In fact, the reason for the problem is that if there are multiple remote warehouse addresses under the same warehouse name, pullonly the first warehouse address will be used to pull content. Therefore, the content of our newly added remote warehouse will not be obtained. That is, it is not pushpossible. So how to solve this problem? It's very simple, change to the first method, use git remote addto add remote warehouses for management. However, pushit is more troublesome. Is there any other way? The answer is: this really exists.

1. Modify the configuration file .git/config

First of all, we know that whether it is git remote addor git remote set-url --addis actually to operate the configuration file in the project .git/config(if you are interested, you can take a look at the content of the configuration file). For git remote set-url --add, the main content of the config configuration file is as follows

[remote "origin"]
        url = https://github.com/onmpw/JYGO.git
        fetch = +refs/heads/*:refs/remotes/github/*
        url = git@localhost:workspace/repo/JYGO2.git

Did you see the content? It's very simple and clear. Since we are pullonly getting the content of the first remote repository, it's not easy to say, just swap the positions of the two, and change as follows

[remote "origin"]
        url = git@localhost:workspace/repo/JYGO2.git
        fetch = +refs/heads/*:refs/remotes/github/*
        url = https://github.com/onmpw/JYGO.git

Then usegit pull

It should be noted here that git pullwhen using it, it is the same as the first method, and --allow-unrelated-historiesthe parameter must also be added

$ git pull
$ git push --all

Very happy that I finally succeeded.

2. Empty Warehouse Method

This method is very simple. Since the content versions are not synchronized, there should be no content. The newly added remote warehouse should be an empty warehouse without any file content. Some people on the Internet say that it is because of README.mdthe file problem. In fact, it is only half right. Because on platforms such as github or gitlab, when a new warehouse is created, some may initialize a README.mdfile by default. For example, github will ask you to choose whether to initialize the file when creating a new warehouse README.md. We just need to not initialize this file. Of course, except for this default file. Before we add this remote warehouse to our project, there should be no files in the warehouse. Only in this way, when we add the remote warehouse to the project, we don’t need pullto push local content directly to the remote warehouse.

Summary: There are many detailed instructions for the use of the commands mentioned above in our git tutorial . You can refer to the corresponding usage during use. I will not go into unnecessary details here.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

How to Append Text to a File Using Bash

Publish Date:2025/04/05 Views:83 Category:OPERATING SYSTEM

We can use the redirection ( ) operator and tee the command to append text to a file. We have to make sure we have enough permissions to add text to the file. If we don't have enough permissions, we may get a permission denied error. Use th

How to add superuser sudo in CentOS

Publish Date:2025/04/05 Views:88 Category:OPERATING SYSTEM

We can only execute certain commands as the root user using sudo the command. The root user is also called sudo a user. In CentOS , we can add users by adding them to wheel the group, or by adding them to /etc/sudoers files under the direct

Add all files in a folder to commit in Git

Publish Date:2025/03/31 Views:159 Category:Git

This article will discuss the necessary steps to add all of your files into one folder for submission. If you have a folder with a dozen files, adding the files one by one can be tedious. Fortunately, Git allows us to add all the contents o

Add the file to the last commit in Git

Publish Date:2025/03/31 Views:177 Category:Git

This article outlines the process of adding a file to the last commit in Git. This comes in handy when you forgot to include a file in the last commit and don't want to create a new file. Let’s get straight to the point. Add the file to t

Configure Git Bash with Visual Studio Code

Publish Date:2025/03/31 Views:138 Category:Git

This article outlines the steps to configure Git Bash with Visual Studio Code on Windows. By default, VSCode uses PowerShell as the integrated terminal. We can configure VSCode to use Git Bash as the integrated terminal. Make sure you have

Adding a remote branch in Git

Publish Date:2025/03/30 Views:142 Category:Git

Git does not allow its developers to create new branches on remote repositories. But instead, we can push an already existing local branch, and after doing so, we can bring it to the remote repository using some Git commands. In every versi

Git remote add SSH

Publish Date:2025/03/30 Views:53 Category:Git

In this day and age, the most widely used version control system is Git, which is operated by most developers within a team structure. This is mainly used to increase code efficiency, no matter how big or critical the project is. In this se

Setting up a Git remote repository

Publish Date:2025/03/29 Views:78 Category:Git

This article will explain how to add or delete remote repositories. A remote repository is a project hosted somewhere, such as Github/Gitlab. Adding a remote repository allows us to use a short name (alias) to execute commands instead of ty

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial