Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Git Remote Repository (Github)

Git Remote Repository (Github)

Git does not have a central server like SVN.

The Git commands we have used so far are all executed locally. If you want to share your code through Git or collaborate with other developers, you need to put the data on a server that other developers can connect to.

This example uses Github as the remote repository. You can first read our concise Github tutorial.


Adding a remote repository

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]

This example uses Github as the remote repository. If you don't have a Github account, you can register on the official website https://github.com/.

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

Use the following command to generate an SSH Key:

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

The following your_email@youremail.com should be changed to the email you registered on Github. After that, you'll be asked to confirm the path and enter a password; here we just press Enter to accept all defaults.

If successful, it will be generated in ~/ under .ssh folder. Go in and open id_rsa.pub, and copy the key。



$ ssh-keygen -t rsa -C "12345678@qq.com"

Generating public/private rsa key pair.

Enter file in which to save the key (/Users/tianqixin/.ssh/id_rsa): 

Enter passphrase (empty for no passphrase):    # 直接回车

Enter same passphrase again:                   # 直接回车

Your identification has been saved in /Users/tianqixin/.ssh/id_rsa.

Your public key has been saved in /Users/tianqixin/.ssh/id_rsa.pub.

The key fingerprint is:

SHA256:MDKVidPTDXIQoJwoqUmI4LBAsg5XByBlrOEzkxrwARI 12345678@qq.com

The key's randomart image is:

+---[RSA 3072]----+

|E*+.+=**oo       |

|%Oo+oo=o. .      |

|%**.o.o.         |

|OO.  o o         |

|+o+     S        |

|.                |

|                 |

|                 |

|                 |

+----[SHA256]-----+

Return to github and go to Account => Settings (account configuration).

Select on the left SSH and GPG keys, then click New SSH key button; set the title as you like, and paste the key generated on your computer.

After a successful addition, the interface is shown below

To verify success, enter the following command:



$ ssh -T git@github.com

The authenticity of host 'github.com (52.74.223.119)' can't be established.

RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.

Are you sure you want to continue connecting (yes/no/[fingerprint])? yes                   # 输入 yes

Warning: Permanently added 'github.com,52.74.223.119' (RSA) to the list of known hosts.

Hi tianqixin! 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 "New repository" as shown below:

Then fill in runoob-git-test (remote repository name) in Repository name, keep the other default settings, and click the "Create repository" button to successfully create a new Git repository:

After successful creation, the following information is displayed:

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

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



$ mkdir runoob-git-test                     # 创建测试目录

$ cd runoob-git-test/                       # 进入测试目录

$ echo "# Runoob Tutorial Git 测试" >> README.md     # 创建 README.md 文件并写入内容

$ ls                                        # 查看目录下的文件

README

$ git init                                  # 初始化

$ git add README.md                         # 添加文件

$ git commit -m "添加 README.md 文件"        # 提交并备注信息

[master (root-commit) 0205aab] 添加 README.md 文件

 1 file changed, 1 insertion(+)

 create mode 100644 README.md



# Commit to Github

$ git remote add origin git@github.com:tianqixin/runoob-git-test.git

$ git push -u origin master

Please copy the following command from where you successfully created the new repository on Github, rather than using the command I provided, because our Github usernames and repository names are different.

Next, we return to the repository created on Github and can see that the file has been uploaded to Github:


Viewing the current remote repository

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

git remote

Example



$ git remote

origin

$ git remote -v

origin    git@github.com:tianqixin/runoob-git-test.git (fetch)

origin    git@github.com:tianqixin/runoob-git-test.git (push)

By adding the -v parameter when executing, you can also see the actual link address of each alias.


Fetching the remote repository

Git has two commands for fetching updates from the remote repository.

1. Download new branches and data from the remote repository:

git fetch

After this command executes, you need to execute git merge to merge the remote branch into your branch.

2. Fetch data from the remote repository and try to merge it into the current branch:

git merge

This command is executed by running git fetch followed immediately by git merge remote branch to any branch you are on.

Suppose you have configured a remote repository and want to fetch updated data; you can first execute git fetch [alias] to tell Git to fetch data it has that you don't, then you can execute git merge [alias]/[branch] to merge any updates on the server (assuming someone pushed to the server at that time) into your current branch.

Next, we click "README.md" on Github and modify it online:

Then we update the modifications locally.



$ git fetch origin

remote: Counting objects: 3, done.

remote: Compressing objects: 100% (2/2), done.

remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0

Unpacking objects: 100% (3/3), done.

From github.com:tianqixin/runoob-git-test

   0205aab..febd8ed  master     -> origin/master

The above information "0205aab..febd8ed master -> origin/master" indicates that the master branch has been updated; we can use the following command to sync the updates locally:



$ git merge origin/master

Updating 0205aab..febd8ed

Fast-forward

 README.md | 1 +

 1 file changed, 1 insertion(+)

View the content of the README.md file:



$ cat README.md 

# Runoob Tutorial Git Test

## First modification content


Pushing to the remote repository

Command to push your new branch and data to a remote repository:



git push [alias] [branch]

The above command pushes your [branch] branch to become the [branch] branch on the [alias] remote repository; an example is shown below.



$ touch runoob-test.txt      # 添加文件

$ git add runoob-test.txt 

$ git commit -m "添加到远程"

master 69e702d] 添加到远程

 1 file changed, 0 insertions(+), 0 deletions(-)

 create mode 100644 runoob-test.txt



$ git push origin master    # 推送到 Github

Returning to our Github repository, you can see the file has been committed:


Deleting a remote repository

To delete a remote repository, you can use the command:

git remote rm [别名]

Example



$ git remote -v

origin    git@github.com:tianqixin/runoob-git-test.git (fetch)

origin    git@github.com:tianqixin/runoob-git-test.git (push)



# Add repository origin2

$ git remote add origin2 git@github.com:tianqixin/runoob-git-test.git



$ git remote -v

origin    git@github.com:tianqixin/runoob-git-test.git (fetch)

origin    git@github.com:tianqixin/runoob-git-test.git (push)

origin2    git@github.com:tianqixin/runoob-git-test.git (fetch)

origin2    git@github.com:tianqixin/runoob-git-test.git (push)



# Delete repository origin2

$ git remote rm origin2

$ git remote -v

origin    git@github.com:tianqixin/runoob-git-test.git (fetch)

origin    git@github.com:tianqixin/runoob-git-test.git (push)