1. Add a remote warehouse
Previously, we only created a Git local warehouse locally. Here we create a Git remote warehouse on GitHub and synchronize the two warehouses remotely. In this way, the warehouse on GitHub can be used as a backup and can be used by others. Collaborate on development through this repository.
1.1. Create a remote warehouse
First log in to GitHub, then find theNew repository
button in the upper right corner to create a new warehouse
Fill in the project name in Repository name, keep other default settings, and click the Create repository
button to successfully create a new Git repository
1.2. Associated with remote warehouse
After completion, the warehouse on GitHub is still empty. GitHub tells us that we can clone a new warehouse from this warehouse, or associate an existing local warehouse with it, and then push the contents of the local warehouse to the GitHub warehouse. .
- The difference between HTTPS and SSH: the former can clone the project on github at will, no matter who owns it; while the latter requires you to be the owner or administrator of the project you want to clone, and you need to add the SSH key first, otherwise you cannot clone it. .
Now, according to GitHub’s prompts, we run the command under the locallearngit
repository:
After is added, use the git remote
or git remote -v
command to view the remote repository associated with the local repository. You can see the name of the remote repository< a i=3>, this is the default name of Git, it can also be changed to something else, but the name will tell you that it is a remote library at a glance. origin
origin
Then GitHub also prompted us to use the local branchgit branch -M <branch-name>
to force the rename before pushing the remote warehouse, because GitHub has changed the default branch name of all newly created warehouses to a>master
has been changed to main
, and the default branch name of the project initialized with git init
is master
, so here It will be recommended that we re-order before pushing.
However, there is no problem if you don’t modify it here. If you push it through thegit push -u origin maste
r method, a master branch will appear in the GitHub warehouse
Push the contents of the local library to the remote. Use thegit push
command to actually push the current branchmaster
to the remote.
Since the remote library is empty, when we pushed the master branch for the first time, we added the -u
parameter. Git will not only push the contents of the local master branch to the remote new master Branch will also associate the local master branch with the remote master branch, which can simplify the commands when pushing or pulling in the future.
1.3. Configure SSH key
However, the above results indicate that there is no access permission. Here we need to generate an ssh key and add it to the GitHub account. First, we need to execute the following command to generate a new key.
ssh-keygen -t rsa -C "[email protected]"
# -t 指定密钥类型,默认是rsa,可以省略
# -C 设置注释文字,比如邮箱
During the above execution process, just pressEnter
. Finally, two files are obtained in the .ssh directory:id_rsa(私有密钥)和id_rsa.pub(公有密钥)
, as above, if you want Log in to the remote warehouse, here we need to add the key in rsa.pub
to GitHub.
First go to the .ssh
directory and find the id_rsa.pub
folder, open it and copy all the contents, then log in to GitHub and enter your Settings
Then you will see these directories on the left, clickSSH and GPG keys
, then click CreateNew SSH key
, and paste the previously copied id_rsa.pub
After the addition is completed, we can then execute thegit push -u origin master
command to push it to the remote warehouse successfully
1.4. Delete the remote warehouse
If you want to delete the remote library, you can use thegit remote rm <name>
command. Before use, it is recommended to use git remote -v
to view the remote library information
However, the "delete" here actually releases the binding relationship between local and remote, and does not physically delete the remote library. There are no changes to the remote library itself. To actually delete the remote library, you need to log in to GitHub, find the delete button on the background page, and then delete it.
2. Clone from the remote repository
Above, we introduced the situation where there is a local library first, then a remote library, and then the remote warehouse is associated. Then there is another situation, that is, we first create a remote library, and then need to clone it from the remote warehouse to the local one.
2.1. Create a remote warehouse
This is the same as above. You still need to log in to GitHub first, and then find the New repository
button in the upper right corner to create a new warehouse
Then fill in the project name in Repository name
. Here we can check Add a README file
so that GitHub will automatically create one for us < a i=3>File, you can see the file after creationREADME.md
2.2. Clone the remote warehouse
After the remote warehouse is created, you can use thegit clone
command to clone a local warehouse. Since we have already configured the SSH key above, you can clone it directly here. Yes