Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Git remote repository


May 25, 2021 Git


Table of contents


Git remote repository

Git doesn't have a central server like SVN.

The Git commands we currently use are executed locally, if you want to share your code with Git or work with other developers. You need to put the data on a server that other developers can connect to.

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


Add a remote library

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

git remote add [shortname] [url]

Take Github as an example as a remote repository, and if you don't have Github, you can https://github.com/ website.

Since the transfer between your local Git repository and the GitHub repository is encrypted through SSH, we need to configure validation information:

Use these commands to generate SSH Key:

$ ssh-keygen -t rsa -C "[email protected]"

The [email protected] change to the mailbox you registered on github, and then you'll ask for confirmation of the path and enter your password, which we'll do with the default all-way carriage return. If it succeeds, it will generate the .ssh folder, go in, open id_rsa.pub, and copy the key inside.

Go back to github, go to Account Settings, and select SSH Keys, Add SSH Key, title, and paste the key generated on your computer.

Git remote repository

To verify success, enter the following command:

$ ssh -T [email protected]
Hi WongJay! You've successfully authenticated, but GitHub does not provide shell access.

The following command indicates that we have successfully connected to Github.

After logging in, click on "New repository" as shown in the following image:

Git remote repository

Then, after filling in the w3cschool.cn (remote repository name) in Therepository name, the other keeps the default settings and clicks on the "Create repository" button to successfully create a new Git repository:

Git remote repository

After the creation is successful, the following information is displayed:

Git remote repository

The above information tells us that we can clone a new repository from this repository or push the contents of the local repository to the GitHub repository.

Now, following GitHub's prompts, we run the command under the local repository:

$ ls
README
W3Cschool教程测试.txt
test.txt
$ git remote add origin [email protected]:WongJay/w3cschool.cn.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 [email protected]:WongJay/w3cschool.cn.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.

The following commands should be replicated where you successfully created the new repository in Github, not according to the commands I provided, because our Github userna name is different and the warehouse name is different.

Next we go back to the repository created by Github and you can see that the file has been uploaded to Github:

Git remote repository


View the current remote library

To see which remote repositories are currently configured, you can use the command:

git remote
$ git remote
origin
$ git remote -v
origin  [email protected]:WongJay/w3cschool.cn.git (fetch)
origin  [email protected]:WongJay/w3cschool.cn.git (push)

Add the -v parameter when executing, and you can also see the actual link address for each alias.


Extract the remote warehouse

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

1, download new branches and data from remote warehouse:

git fetch

After the command is executed, you need to execute the git merge 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 followed by the execution of git fetch followed by the execution of the git merge remote branch to any branch you are in.

Assuming that you have configured a remote repository and you want to extract the updated data, you can first execute git fetch (alias) to tell Git to get the data you don't have, and then you can execute git merge (alias)/branch) to merge any updates on the server (assuming someone pushes to the server at this point) into your current branch.

Next we click on "w3cschoolW3Cschool tutorial test .txt" on Github and modify it online. We then update the modification 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:WongJay/w3cschool.cn
   7d2081c..f5f3dd5  master     -> origin/master

The above information "7d2081c: f5f3dd5 master -gt; origin/master" indicates that the master branch has been updated and we can sync the update locally using the following command:

$ 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 remote warehouse

Push your new branch with data to a remote repository command:

git push [alias] [branch]

The above command pushes your branch to the branch of the branch on the remote repository of the branch, as shown below.

$ 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 WongJay$ vim w3cschoolW3Cschool教程测试.txt 
bogon:w3cschoolcc WongJay$ git push origin master
Everything up-to-date

Delete the remote warehouse

To delete a remote repository you can use commands:

git remote rm [别名]
$ git remote -v
origin  [email protected]:WongJay/w3cschool.cn.git (fetch)
origin   [email protected]:WongJay/w3cschool.cn.git (push)
$ git remote add origin2 [email protected]:WongJay/w3cschool.cn.git
$ git remote -v
origin   [email protected]:WongJay/w3cschool.cn.git (fetch)
origin   [email protected]:WongJay/w3cschool.cn.git (push)
origin2   [email protected]:WongJay/w3cschool.cn.git (fetch)
origin2  [email protected]:WongJay/w3cschool.cn.git (push)
$ git remote rm origin2
$ git remote -v
origin  [email protected]:WongJay/w3cschool.cn.git (fetch)
origin   [email protected]:WongJay/w3cschool.cn.git (push)