This is the 28th day of my participation in the August Text Challenge.More challenges in August

preface

In actual development, we sometimes encounter situations where a local repository is associated with multiple remote repositories. So, how can we easily switch and synchronize code between two repositories? Unfamiliar partners, in the face of this problem, must be full of question marks? Today, this article will give you a popular science wave.

The body of the

Typically, after we create a new repository on GitHub, GitHub provides three sample ways to do this in general. Shows how to associate a local project with the GitHub repository.

Here’s a brief description of the three cases.

Case one, there is a local project directory

This situation is relatively simple. We just need to synchronize the cloud warehouse, and the project directory will be automatically created locally. The operation method and command are as follows:

git clone [email protected]:LLL/gloud/bominio.git
cd bominio
touch README.md
git add README.md
git commit -m "add README"
git push -u origin master
Copy the code

Case 2. The project directory already exists

In this case, go to the corresponding project directory and run the git init command. The operation method and command are as follows:

cd existing_folder
git init
git remote add origin [email protected]:LLL/gloud/bominio.git
git add .
git commit -m "Initial commit"
git push -u origin master
Copy the code

Case 3. The project directory already exists locally and has been associated with other warehouses

And that’s what we’re going to talk about today. The main difficulty is to define the name of the remote warehouse and switch the warehouse. First, the operation method and command are as follows:

cd existing_repo
git remote rename origin old-origin
git remote add origin [email protected]:LLL/gloud/bominio.git
git push -u origin --all
git push -u origin --tags
Copy the code

When we mix up the warehouse, it is easy to mistakenly push the code belonging to warehouse A to warehouse B, resulting in errors. Common errors are as follows:

Fatal: Unable to access ‘github.com/minio/minio… ‘: LibreSSL SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443

To solve this situation, we need to understand the management of the remote warehouse. Here is an example of how to change the name of a remote repository. Git remote -v git remote -v git remote -v git remote -v

localhost:bominio lz$ git remote -v

borigin [email protected]:LLL/gloud/bominio.git (fetch)

borigin [email protected]:LLL/gloud/bominio.git (push)

Origin github.com/minio/minio… (fetch)

Origin github.com/minio/minio… (push)

From the output above, we can see that the Bominio repository is associated with two remote repositories, Borigin and Origin. At this point, if we want to change the name of our warehouse borigin to Origin, what should we do? At this point, I suddenly thought of a super simple pen test: write an algorithm that swaps the values of variable A and variable B. Yes, the principle is similar! We need to change the name of origin warehouse to another name first, and then change our own warehouse to Origin. The specific operations are as follows:

// Change the name of the origin repository to another name

localhost:bominio lz$ git remote rename origin old-origin

localhost:bominio lz$ git remote -v

borigin [email protected]:LLL/gloud/bominio.git (fetch)

borigin [email protected]:LLL/gloud/bominio.git (push)

Old – origin github.com/minio/minio… (fetch)

Old – origin github.com/minio/minio… (push)

To change the name of your repository, run the following command:

localhost:bominio lz$ git remote rename borigin origin

At this point, we look at the names of the two remote repositories and find that we are OK.

localhost:bominio lz$ git remote -v

Old – origin github.com/minio/minio… (fetch)

Old – origin github.com/minio/minio… (push)

origin [email protected]:LLL/gloud/bominio.git (fetch)

origin [email protected]:LLL/gloud/bominio.git (push)

Now that the name change is done, let’s talk about synchronizing the code. Now that we can distinguish between remote repositories by name, we need to specify the repository object for each command when we synchronize the code. For example, the remote repository code can be synchronized locally only by executing Git pull. However, when multiple remote repositories exist, git pull Origin is required.

At the end

That’s enough about using Git to play with multiple remote repositories. In fact, many problems are like this, not difficult, you just don’t know. If you are interested, try it! I am Liuzhen007 (alias: data-mining), welcome to leave a comment + one button three connect.

Calendar Clocking (August Challenge)