Now, after you've created a git repository locally, you want to create a git repository on GitHub and synchronize the two warehouses remotely, so that the repository on GitHub can be used as a backup and other people to collaborate through the warehouse.
First, log on to GitHub, and then, in the upper right corner, find the "Create a new Repo" button to create a new warehouse:
In the repository name fill in learngit
, the other remains the default settings, click the "Create Repository" button, the successful creation of a new Git repository:
At the moment, this warehouse on GitHub learngit
is still empty, and GitHub tells us that we can clone a new repository from this repository, or associate an existing local repository with it, and then push the contents of the local repository to the GitHub repository.
Now, let's run the command under the local repository, based on GitHub's tips learngit
:
$ git remote add origin git@github.com:michaelliao/learngit.git
Please be careful to replace the above with michaelliao
your own GitHub account name, otherwise, you are locally associated with my Remote library, the association is not a problem, but you later push is not pushed up, because your SSH key public key is not in my account list.
After adding, the name of the remote library is origin
, this is the default for Git, or can be changed to another, but origin
this name is known as a remote library.
Next, you can push all the contents of the local library to the remote library:
$ git push -u origin masterCounting objects: 19, done.Delta compression using up to 4 threads.Compressing objects: 100% (19/19), done.Writing objects: 100% (19/19), 13.73 KiB, done.Total 23 (delta 6), reused 0 (delta 0)To [email protected]:michaelliao/learngit.git * [new branch] set up to track remote branch master from origin.
Pushing the contents of the local library to the remote, with git push
the command, is actually pushing the current branch master
to the remote.
Since the remote library is empty, the first time we push master
a branch, with -u
parameters, Git will not only master
push the local branch content of the remote new master
branch, but also the local master
branch and the Remote master
Branch Association, You can simplify the command at a later push or pull.
Once the push is successful, you can see the remote library's content on the GitHub page immediately as if it were local:
From now on, as long as the local commits, you can pass the command:
$ git push origin master
Push the master
latest changes from your local branch to GitHub, and now you have a truly distributed repository!
SSH warning
The first time you use Git clone
or push
a command to connect to GitHub, you get a warning:
The authenticity of host ‘github.com (xx.xx.xx.xx)‘ can‘t be established.RSA key fingerprint is xx.xx.xx.xx.xx.Are you sure you want to continue connecting (yes/no)?
This is because Git uses SSH connection, and SSH connection is the first time to verify the GitHub server key, you need to confirm that GitHub key fingerprint information is really from the GitHub server, enter the yes
return.
Git will output a warning telling you that you have added GitHub key to a trust list on this machine:
‘github.com‘ (RSA) to the list of known hosts.
This warning will only appear once, and there will be no warning after the operation.
If you're really worried about someone impersonating a github server, you yes
can match the fingerprint information of GitHub's RSA key with the SSH connection before entering it.
Summary
To associate a remote library, use the command git remote add origin [email protected]:path/repo-name.git
;
Once associated, use the command to git push -u origin master
push all the contents of the master branch for the first time;
Thereafter, after each local submission, whenever necessary, you can use the command to git push origin master
push the latest changes;
One of the biggest benefits of distributed version systems is that working locally does not have to take into account the existence of remote libraries, which means that there is no Internet connection that works, and SVN refuses to work when it is not connected! When there is a network, and then the local submission push a bit to complete the synchronization, it is very convenient!
To add a remote library