GitHub
- [1. 创建远程仓库](#1. 创建远程仓库)
- [2. 远程仓库操作](#2. 远程仓库操作)
-
- [1. 创建远程仓库别名](#1. 创建远程仓库别名)
- [2. 推送本地分支到远程仓库](#2. 推送本地分支到远程仓库)
- [3. 拉取远程仓库到本地库](#3. 拉取远程仓库到本地库)
- [4. 克隆远程仓库到本地](#4. 克隆远程仓库到本地)
- [5. 邀请加入团队](#5. 邀请加入团队)
- 3.跨团队协作
- [4. SSH免密登录](#4. SSH免密登录)
GitHub官网: https://github.com/
1. 创建远程仓库
远程库最好和本地库的名字一致。
Public:仓库类型是公有的,所有人都可以看到,你可以选择谁能提交。
Private:仓库类型是私有的,可以选择谁能看到,谁能提交。
远程库提供两种链接方式:HTTPS和SSH。
2. 远程仓库操作
命令名称 | 作用 |
---|---|
git remote -v | 查看当前所有远程地址别名 |
git remote add 别名 远程地址 | 起别名 |
git push 别名 分支 | 推送本地分支上的内容到远程仓库 |
git clone 远程地址 | 将远程仓库的内容克隆到本地 |
git pull 远程库地址别名 远程分支名 | 将远程仓库对于分支最新内容拉下来后与当前本地分支直接合并 |
1. 创建远程仓库别名
- 基本语法
bash
git remote -v 查看当前所有远程地址别名
git remote add 别名 远程地址
- 案例
bash
1. L@DESKTOP-C3ML9EI MINGW64 /e/Git-space/GitToGitHub (master)
2. $ git remote -v
3.
4. L@DESKTOP-C3ML9EI MINGW64 /e/Git-space/GitToGitHub (master)
5. $ git remote add GitToGitHub https://github.com/XXX/GitToGitHub.git
6.
7. L@DESKTOP-C3ML9EI MINGW64 /e/Git-space/GitToGitHub (master)
8. $ git remote -v
9. GitTo-GitHub https://github.com/XXX/GitToGitHub.git (fetch)
10. GitTo-GitHub https://github.com/XXX/GitToGitHub.git (push)
2. 推送本地分支到远程仓库
- 基本语法
bash
git push 别名 分支
- 案例
bash
1. L@DESKTOP-C3ML9EI MINGW64 /e/Git-space/GitToGitHub (master)
2. $ git push GitToGitHub master
3. Enumerating objects: 27, done.
4. Counting objects: 100% (27/27), done.
5. Delta compression using up to 8 threads
6. Compressing objects: 100% (20/20), done.
7. Writing objects: 100% (27/27), 2.44 KiB | 624.00 KiB/s, done.
8. Total 27 (delta 6), reused 0 (delta 0), pack-reused 0
9. remote: Resolving deltas: 100% (6/6), done.
10. To https://github.com/SmallGarden/GitToGitHub.git
11. * [new branch] master -> master
*Note:本地库与远程库第一次建立联系需要其中的代码拉取过,或者远程库中为空。
3. 拉取远程仓库到本地库
- 基本语法
bash
git pull 别名 分支
- 实例
bash
1. $ git pull GitToGitHub master
2. remote: Enumerating objects: 5, done.
3. remote: Counting objects: 100% (5/5), done.
4. remote: Compressing objects: 100% (3/3), done.
5. remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
6. Unpack-ing objects: 100% (3/3), 682 bytes | 4.00 KiB/s, done.
7. From https://github.com/SmallGarden/GitToGitHub
8. * branch master -> FETCH_HEAD
9. 85d73d2..87c88ca master -> GitToGitHub/master
10. Updating 85d73d2..87c88ca
11. Fast-forward
12. hello.txt | 1 +
13. 1 file changed, 1 insertion(+)
在远程库中修改一个文件,然后拉取到本地库中,将覆盖本地库中的文件。
4. 克隆远程仓库到本地
克隆代码不需要登入账号。
- 基本语法
bash
git clone 远程地址
- 实例
bash
1. L@DESKTOP-C3ML9EI MINGW64 /e/Git-space/Test
2. $ git clone https://github.com/XXX/GitToGitHub.git
3. Cloning into 'GitToGitHub'...
4. remote: Enumerating objects: 30, done.
5. remote: Counting objects: 100% (30/30), done.
6. remote: Compressing objects: 100% (17/17), done.
7. remote: Total 30 (delta 7), reused 26 (delta 6), pack-reused 0
8. Receiving objects: 100% (30/30), done.
9. Resolving deltas: 100% (7/7), done.
这个地址为远程仓库地址,克隆结果:顺便初始化本地仓库,并创建远程仓库别名。
bash
1. L@DESKTOP-C3ML9EI MINGW64 /e/Git-space/Test/GitToGitHub (master)
2. $ git remote -v
3. origin https://github.com/XXX/GitToGitHub.git (fetch)
4. origin https://github.com/XXX/GitToGitHub.git (push)
小结:clone会做如下操作:1、拉取代码;2、初始化本地库;3、创建别名。
5. 邀请加入团队
-
选择邀请合作者
搜索邀请之后,复制邀请链接发送到邀请人。被邀请人在浏览器中输入邀请链接后加入远程库开发团队。
直接在原作者的远程库中修改和推送代码,不是在自己的远程库中操作。 -
修改后推送
3.跨团队协作
邀请不是自己团队的人员进行维护代码。
-
通过链接或者搜索定位到相对应的远程库;
-
将别人的公开远程库Fork到自己的远程库中;
-
编辑修改代码:可以在线下或者线上编辑,然后将代码重新提交到自己的远程库中;(团队内协作可以直接提交到维护者的远程库中)
-
将进行的修改需要提交拉取请求pull request
-
源代码维护者将查看pull request:查看修改代码和交流(github中可实时交流)
-
代码维护者在远程库中合并修改的代码
4. SSH免密登录
我们可以看到远程仓库中还有一个SSH的地址,因此我们也可以实用SSH 进行访问。
- 首先删除已经有的.ssh文件夹及其中的文件;
bash
L@DESKTOP-C3ML9EI MINGW64 ~
$ rm -rvf .ssh
removed '.ssh/id_rsa'
removed '.ssh/id_rsa.pub'
removed '.ssh/known_hosts'
removed '.ssh/known_hosts.old'
removed directory '.ssh'
- 在系统盘用户目录下生成SSH密钥
bash
1. L@DESKTOP-C3ML9EI MINGW64 ~
2. $ ssh-keygen -t rsa -C XXX@qq.com
3. Generating public/private rsa key pair.
4. Enter file in which to save the key (/c/Users/L/.ssh/id_rsa):
5. Created directory '/c/Users/L/.ssh'.
6. Enter passphrase (empty for no passphrase):
7. Enter same passphrase again:
8. Your identification has been saved in /c/Users/L/.ssh/id_rsa
9. Your public key has been saved in /c/Users/L/.ssh/id_rsa.pub
10. The key fingerprint is:
11. SHA256:+vCLX8bRSFxe2Dx9giYUvWzGT31LWd7ZPSShU/VcrX4 270717808@qq.com
12. The key's randomart image is:
13. +---[RSA 3072]----+
14. | .oo.B+oo|
15. | ..oBo=oB|
16. | oB..o*@|
17. | . oB o=B|
18. | S oo.+. +|
19. | . . . o.E|
20. | o + . |
21. | = o |
22. | ..=. |
23. +----[SHA256]-----+
- 查看SSH密钥;
bash
L@DESKTOP-C3ML9EI MINGW64 ~/.ssh
$ cat id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2 ......
- 在GitHub账号中关联当前windows系统中用户生成的SSH密钥;
- 利用SSH密钥链接拉取远程库代码
bash
L@DESKTOP-C3ML9EI MINGW64 /e/Git-space/GitToGitHub (master)
$ git pull git@github.com:SmallGarden/GitToGitHub.git master
The authenticity of host 'github.com (20.205.243.166)' can't be established.
ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warn-ing: Permanently added 'github.com' (ED25519) to the list of known hosts.
remote: Enumerating objects: 6, done.
remote: Counting objects: 100% (6/6), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 4 (delta 1), reused 0 (delta 0), pack-reused 0
Unpack-ing objects: 100% (4/4), 1.27 KiB | 2.00 KiB/s, done.
From github.com:SmallGarden/GitToGitHub
* branch master -> FETCH_HEAD
Updating 87c88ca..efe977b
Fast-forward
hello.txt | 1 +
1 file changed, 1 insertion(+)
- 利用SSH密钥链接推送远程库代码
bash
L@DESKTOP-C3ML9EI MINGW64 /e/Git-space/GitToGitHub (master)
$ git push git@github.com:SmallGarden/GitToGitHub.git master
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 322 bytes | 322.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To github.com:SmallGarden/GitToGitHub.git
efe977b..b6f9e99 master -> master