Back up the code to Gitee and GitHub at the same time

Posted by DGaleDavid on Fri, 21 Jan 2022 21:32:04 +0100

Back up the code to Gitee and GitHub at the same time

How to import a GitHub project into Gitee in one step
How to keep Gitee and GitHub updated synchronously

How to import a GitHub project into Gitee in one step

Method 1:

Log in to Gitee's account, click the + sign in the upper right corner, click "import project from GitHub", and authorize Gitee to access in the jump page.

Then selectively import from GitHub

Method 2:

When creating a project, select to import an existing project.

If it is a private project, you will need to enter the account information of GitHub.

How to keep Gitee and GitHub updated synchronously

Method 1: use Gitee's forced synchronization

I still only need to maintain the source code of github. If gitee doesn't forget, just rub your hands and click the forced synchronization button.

However, it is easy to forget, resulting in incomplete synchronization between the two sides.

Remember that only warehouses imported from GitHub have the button to force synchronization

Method 2: rub and push multiple times by hand

It's nothing more than configuring multiple remote library addresses and pushing multiple times. Let's take a look at the existing remote libraries (take WhiteHole as an example)

$ git remote --verbose
origin  https://gitee.com/BlackThompson/white-hole.git (fetch)
origin  https://gitee.com/BlackThompson/white-hole.git (push)

You can see that there are only https://gitee.com/BlackThompson/white-hole.git This remote library address

Let's add a gitee remote address. First, build a synchronization warehouse in gitee, and then add a new remote library address locally:

$ git remote add githuborigin https://github.com/BlackThompson/WhiteHole

After adding, let's check:

$ git remote --verbose
githuborigin    https://github.com/BlackThompson/WhiteHole (fetch)
githuborigin    https://github.com/BlackThompson/WhiteHole (push)
origin  https://gitee.com/BlackThompson/white-hole.git (fetch)
origin  https://gitee.com/BlackThompson/white-hole.git (push)

You can view the following 2 remote library addresses:

githuborigin: is the remote library address of our newly added github
origin: it's the address of our remote library in gitee
Next, synchronize:

git add .
git commit -m "add gitee"
git push -u origin master
git push -u githuborigin master

One more git push operation than before... Nothing much different from before... No more mental burden

But often easy to forget

Method 3: run once at most
coder, who doesn't want to be lazy, is not a good programmer. Adhering to the concept of "run once at most", let's try how to push everything at one time

Find this file in the local git repository git/config, as follows:

[core]
	repositoryformatversion = 0
	filemode = false
	bare = false
	logallrefupdates = true
	symlinks = false
	ignorecase = true
[remote "origin"]
	url = https://gitee.com/BlackThompson/white-hole.git	
	fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
	remote = origin
	merge = refs/heads/master
[gui]
	wmstate = normal
	geometry = 1205x669+38+38 276 304
[remote "githuborigin"]
	url = https://github.com/BlackThompson/WhiteHole
	fetch = +refs/heads/*:refs/remotes/githuborigin/*

Replace with the following:

Merge 2 remote configurations

[core]
	repositoryformatversion = 0
	filemode = false
	bare = false
	logallrefupdates = true
	symlinks = false
	ignorecase = true
[remote "origin"]
	url = https://github.com/BlackThompson/WhiteHole
	url = https://gitee.com/BlackThompson/white-hole.git
	fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
	remote = origin
	merge = refs/heads/master

The above manual configuration is just for better explanation. In fact, the following commands can be used to simplify the operation, and a new remote address is added under the origin node

$ git remote set-url --add origin https://github.com/BlackThompson/WhiteHole

Look at the supplemented remote address

$ git remote --verbose
origin  https://gitee.com/BlackThompson/white-hole.git (fetch)
origin  https://gitee.com/BlackThompson/white-hole.git (push)
origin  https://github.com/BlackThompson/WhiteHole (push)

Pay attention to the fetch (push) in the back. I'm sure you'll understand something

Then we can continue to use this method to realize synchronous push and distribution of GitHub & gitee:

git add .
git commit -m "github & gitee Synchronous push and distribution"
git push origin master

It can be seen that there is no difference between the use and the initial use. It is only configured once more, which can be regarded as the realization of "matching (running) once at most"

Method 4: the command line pulls the code on GitHub and imports it into Gitee
If it is a local warehouse, only Gitee and Github remote libraries with different names need to be added on the command line.

git remote add remote library name remote library address

The specific methods are as follows:

1. First, check the remote database list of the warehouse you want to synchronize through git remote -v. if there is no remote database address of your code cloud in the list, you need to add an address

git remote add remote library name remote library address
eg: git remote add gitee git@github.com:xxx/xxx.git

If error occurs during add: could not remove config section 'remote xxx’. A kind of mistake by putting the warehouse down Delete [remote "XXX"] in git/config file or use another remote library name.

2. Pull the latest code from GitHub to the local

git pull remote library name branch name
eg: git pull origin master

3. Push the latest local code to the code cloud

git push remote library name branch name
eg: git push gitee master

If there is any difference, you need to solve the difference manually

Reference article:

How to import GitHub project into code cloud? One step!

How to synchronize multiple git remote warehouses
--------
Original link: https://blog.csdn.net/qq_51771849/article/details/114179329