Git remote warehouse
Git does not have a central server like SVN.
The Git commands we currently use are all executed locally, if you want to share your code or collaborate with other developers through Git. You then need to put the data on a server that other developers can connect to.
This example uses Github as the remote warehouse. You can read our Github concise tutorial first.
Add remote library
To add a new remote warehouse, you can specify a simple name for future reference. The command format is as follows:
git remote add [shortname] [url]
This example uses Github as the remote warehouse. If you don’t have Github, you can register on the official website https://github.com/.
Since the transfer between your local Git repository and GitHub repository is encrypted through SSH, we need to configure the authentication information:
Use the following command Generate SSH Key:
$ ssh-keygen -t rsa -C "youremail@example.com"
Change your_email@youremail.com to the email you registered on github. You will then be asked to confirm the path and enter a password. We just use the default one and press Enter all the way. If successful, a .ssh folder will be generated under ~/, go in, open id_rsa.pub, and copy the key inside.
Go back to github, enter Account Settings, select SSH Keys on the left, Add SSH Key, fill in the title as you like, and paste the key generated on your computer.
In order to verify success, enter the following command:
$ ssh -T git@github.com Hi tianqixin! You've successfully authenticated, but GitHub does not provide shell access.
The following command shows that we have successfully connected to Github.
After logging in, click "New repository" as shown below:
Then fill in w3cschool.cc (remote warehouse name) in the Repository name. , keep the other default settings, click the "Create repository" button, and a new Git repository is successfully created:
After the creation is successful, the following information is displayed:
The above information tells us that we can clone a new warehouse from this warehouse, or push the contents of the local warehouse to the GitHub warehouse.
Now, we follow the prompts of GitHub and run the command under the local warehouse:
$ ls README w3cschoolphp中文网测试.txt test.txt $ git remote add origin git@github.com:tianqixin/w3cschool.cc.git $ git push -u origin master Counting objects: 21, done. Delta compression using up to 4 threads. Compressing objects: 100% (15/15), done. Writing objects: 100% (21/21), 1.73 KiB | 0 bytes/s, done. Total 21 (delta 4), reused 0 (delta 0) To git@github.com:tianqixin/w3cschool.cc.git * [new branch] master -> master Branch master set up to track remote branch master from origin.
Please copy the following commands according to where you successfully created a new warehouse on Github, not according to what I provided. command, because our Github user names are different and the warehouse names are also different.
Next we return to the warehouse created by Github, and you can see that the file has been uploaded to Github:
View the current remote library
To To see which remote warehouses are currently configured, you can use the command:
git remote
$ git remote origin $ git remote -v origin git@github.com:tianqixin/w3cschool.cc.git (fetch) origin git@github.com:tianqixin/w3cschool.cc.git (push)
Add the -v parameter when executing, and you can also see the actual link address of each alias.
Extract remote repository
Git has two commands used to extract updates from remote repository.
1. Download new branches and data from the remote warehouse:
git fetch
After executing this command, you need to execute git merge from the remote branch to your branch.
2. Extract data from the remote warehouse and try to merge it into the current branch:
git pull
This command is to execute git fetch and then execute git merge to transfer the remote branch to any branch you are on.
Assuming you have configured a remote warehouse and you want to fetch updated data, you can first execute git fetch [alias] Tell Git to fetch the data it has that you don't have, and then you can do git merge [alias]/[branch] to merge any updates on the server (assuming someone has pushed to the server at this time) into your the current branch.
Next we click "w3cschoolphp Chinese network test.txt" on Github and modify it online. Then we update and modify it locally.
$ git fetch origin Warning: Permanently added the RSA host key for IP address '192.30.252.128' to the list of known hosts. remote: Counting objects: 3, done. remote: Compressing objects: 100% (2/2), done. remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0 Unpacking objects: 100% (3/3), done. From github.com:tianqixin/w3cschool.cc 7d2081c..f5f3dd5 master -> origin/master
The above information "7d2081c..f5f3dd5 master -> origin/master" indicates that the master branch has been updated. We can use the following command to synchronize the update to the local:
$ git merge origin/master Updating 7d2081c..f5f3dd5 Fast-forward "w3cschool074107651703653075.txt" | 1 + 1 file changed, 1 insertion(+)
Push to the remote warehouse
Push your new branch and data to a remote warehouse command:
git push [alias] [branch]
The above command will push your [branch] branch to the [alias] remote warehouse [branch] branch, examples are as follows.
$ git merge origin/master Updating 7d2081c..f5f3dd5 Fast-forward "w3cschool074107651703653075.txt" | 1 + 1 file changed, 1 insertion(+) bogon:w3cschoolcc tianqixin$ vim w3cschoolphp中文网测试.txt bogon:w3cschoolcc tianqixin$ git push origin master Everything up-to-date
Delete the remote warehouse
To delete the remote warehouse you can use the command:
git remote rm [别名]
$ git remote -v origin git@github.com:tianqixin/w3cschool.cc.git (fetch) origin git@github.com:tianqixin/w3cschool.cc.git (push) $ git remote add origin2 git@github.com:tianqixin/w3cschool.cc.git $ git remote -v origin git@github.com:tianqixin/w3cschool.cc.git (fetch) origin git@github.com:tianqixin/w3cschool.cc.git (push) origin2 git@github.com:tianqixin/w3cschool.cc.git (fetch) origin2 git@github.com:tianqixin/w3cschool.cc.git (push) $ git remote rm origin2 $ git remote -v origin git@github.com:tianqixin/w3cschool.cc.git (fetch) origin git@github.com:tianqixin/w3cschool.cc.git (push)