Latest web development tutorials

Git remote repository

Git is not that there is a central server like SVN.

We are currently using to Git commands are executed locally, Git if you want to share your code or in cooperation with other developers. You need to put data on one server can connect to other developers.

This example uses the Github repository as a remote, you can read our first Github simple tutorial.


Adding remote libraries

To add a new remote repository, you can specify a simple name, for future reference, the command format is as follows:

git remote add [shortname] [url]

In this example, a remote repository Github as an example, if you can not Github official website https://github.com/ registered.

Since the transmission of your local Git repository and GitHub repository is via SSH encryption, so we need to configure the authentication information:

Use the following command to generate SSH Key:

$ ssh-keygen -t rsa -C "youremail@example.com"

Your_email@youremail.com back to your mailbox registered on github, then you will be asked to confirm the path and enter the password, which we use the default way to enter the line. Successful will generate .ssh folder ~ / under, inside, open id_rsa.pub, copy inside the key.

Back on github, enter Account Settings (account configuration), the left select SSH Keys, Add SSH Key, title just fill pasted generated on your computer key.

To verify successful, 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 login and click "New repository" as shown below:

After the Repository name filled w3cschool.cc (remote repository name), others keep the default settings, click on "Create repository" button, we successfully created a new Git repository:

Once created, the following information is displayed:

The above information we can tell from this repository clone new warehouse, the content may be pushed to local repository GitHub repository.

Now, follow the prompts to GitHub, and run the command in a local warehouse:

$ ls
README
w3cschool本教程测试.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.

The following commands according to your local copy in Github successfully created a new warehouse, rather than command me, because our Github username is not the same, not the same repository name.

Next we create a return Github repository, you can see the files have been uploaded to Github:


Check the current remote library

To see what the current remote repository is arranged, 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)

With the -v parameter, you can also see the actual link address for each alias when executed.


Extract a remote repository

Git has two commands used to extract the remote repository updates.

1. Download the new branch with the data from a remote repository:

git fetch

The need to perform after being run git merge a remote branch into your branch is located.

2, extract data from a remote warehouse and try to merge into the current branch:

git pull

The command is executed in any branch in your git fetch immediately after the implementation of the remote branch to the git merge.

Suppose you have configured a remote repository, and you want to extract the updated data, you can run git fetch [alias] tell Git to get it you do not have the data, and then you can run git merge [alias] / [branch first ] to any updates on the server (assuming that this time it was pushed to the server) into your current branch.

Then we click on "w3cschool tutorial test .txt" on Github and modify it online. Once we've updated the changes 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" master branch instructions have been updated, we can use the following command to update synchronized to a local:

$ git merge origin/master
Updating 7d2081c..f5f3dd5
Fast-forward
 "w3cschool\350\217\234\351\270\237\346\225\231\347\250\213\346\265\213\350\257\225.txt" | 1 +
 1 file changed, 1 insertion(+)

Push to a remote repository

Push your new branch and the data warehouse to a distal end of the command:

git push [alias] [branch]

The above command you [branch] branch push to become [branch] branch, examples [alias] remote repository is as follows.

$ git merge origin/master
Updating 7d2081c..f5f3dd5
Fast-forward
 "w3cschool\350\217\234\351\270\237\346\225\231\347\250\213\346\265\213\350\257\225.txt" | 1 +
 1 file changed, 1 insertion(+)
bogon:w3cschoolcc tianqixin$ vim w3cschool本教程测试.txt 
bogon:w3cschoolcc tianqixin$ git push origin master
Everything up-to-date

Delete a remote repository

Deleting remote repository 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)