GitHub 网址: https://github.com/
相信有很多朋友会发现访问github网站速度是非常慢的
这里推荐一个github加速工具FastGithub,地址:https://github.com/dotnetcore/FastGithub
里面有详细的使用步骤。
首先要登录github,没有可以注册一个。
创建远程仓库第一步:点击+ ,选择New repository
第二步:填写你的仓库名,点击创建
一般远程库的名称是和自己本地库的名称相同。
因为在之前已经创建了一个test的本地库,所以远程库也命名为test
创建好后,有两个链接选择
HTTPS:https://github.com/dongguo4812/test.git
SSH:git@github.com:dongguo4812/test.git
远程仓库操作 命令名称作用git remote -v查看当前所有远程地址别名git remote add 别名 远程地址起别名git push 别名 分支推送本地分支上的内容到远程仓库git clone 远程地址将远程仓库的内容克隆到本地git pull 远程库地址别名 远程分支名将远程仓库对于分支最新内容拉下来后与 当前本地分支直接合并 创建远程仓库别名 1) 基本语法git remote -v 查看当前所有远程地址别名
git remote add 别名 远程地址
#没有设置别名
Administrator@WIN-NNHMK0M7785 MINGW64 /e/test (master)
$ git remote -v
#设置https://github.com/dongguo4812/test.git的别名为test
Administrator@WIN-NNHMK0M7785 MINGW64 /e/test (master)
$ git remote add test https://github.com/dongguo4812/test.git
#在执行fetch(clone、pull)和push操作都可以使用别名代替远程地址
Administrator@WIN-NNHMK0M7785 MINGW64 /e/test (master)
$ git remote -v
test https://github.com/dongguo4812/test.git (fetch)
test https://github.com/dongguo4812/test.git (push)
https://github.com/dongguo4812/test.git就是test远程库对应的HTTPS链接
推送本地分支到远程仓库 1) 基本语法git push 别名 分支
2) 案例实操
本地的代码都已经commit到本地库中,可以直接push到远程库。
Administrator@WIN-NNHMK0M7785 MINGW64 /e/test (master)
$ git push test master
Enumerating objects: 13, done.
Counting objects: 100% (13/13), done.
Delta compression using up to 4 threads
Compressing objects: 100% (9/9), done.
Writing objects: 100% (13/13), 1.05 KiB | 358.00 KiB/s, done.
Total 13 (delta 3), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (3/3), done.
To https://github.com/dongguo4812/test.git
* [new branch] master -> master
如果你是第一次推送到github需要登录
可以选择Sign in with browser,将git与github账号绑定授权。
此时发现已将我们 master 分支上的内容推送到 GitHub 创建的远程仓库。
当远程仓库代码更新后,需要将远程仓库代码拉取到本地分支
1) 基本语法git pull 别名 分支
2) 案例实操
在github上直接修改hello.txt模拟其他成员push代码导致远程库代码发生变更
新增 远程仓库代码变更
commit changes
拉取远程仓库代码到本地分支
Administrator@WIN-NNHMK0M7785 MINGW64 /e/test (master)
$ git pull test master
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), 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), 727 bytes | 4.00 KiB/s, done.
From https://github.com/dongguo4812/test
* branch master -> FETCH_HEAD
23aa6ad..888b61d master -> test/master
Updating 23aa6ad..888b61d
Fast-forward
hello.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
查看hello.txt
Administrator@WIN-NNHMK0M7785 MINGW64 /e/test (master)
$ cat hello.txt
hello git123456789
hello git111111111
hello git
hello git
hello git
hello git
hello git
hello git
远程仓库代码变更
本地库已经与远程库的代码同步。
克隆远程仓库到本地 1) 基本语法git clone 远程地址
2)案例实操
创建一个新的文件夹test-git
右键Git Bash Here
Administrator@WIN-NNHMK0M7785 MINGW64 /e/test-git
$ git clone https://github.com/dongguo4812/test.git
Cloning into 'test'...
remote: Enumerating objects: 16, done.
remote: Counting objects: 100% (16/16), done.
remote: Compressing objects: 100% (8/8), done.
remote: Total 16 (delta 4), reused 12 (delta 3), pack-reused 0
Receiving objects: 100% (16/16), done.
Resolving deltas: 100% (4/4), done.
tips:只克隆代码是不需要账号的
已经将远程库的test克隆到本地,进行了初始化
查看别名
Administrator@WIN-NNHMK0M7785 MINGW64 /e/test-git
$ cd test
Administrator@WIN-NNHMK0M7785 MINGW64 /e/test-git/test (master)
$ git remote -v
origin https://github.com/dongguo4812/test.git (fetch)
origin https://github.com/dongguo4812/test.git (push)
小结: clone 会做如下操作。 1、拉取代码。 2、初始化本地仓库。 3、创建别名
默认别名为origin
邀请加入团队 团队内合作 1) 选择邀请合作者点击view invitation跳转到github
那么在dongguo-test1的账户上就可以看到test的远程库
在dongguo4812的账户上就能看到test的团队合作者dongguo-test1
Administrator@WIN-NNHMK0M7785 MINGW64 /e/test-git/test (master)
$ vim hello.txt
新增一行 1111111111 by dongguo-test1
hello git123456789
hello git111111111
hello git
hello git
hello git
hello git
hello git
hello git
远程仓库代码变更
1111111111 by dongguo-test1
#将编辑好的文件添加到暂存区
Administrator@WIN-NNHMK0M7785 MINGW64 /e/test-git/test (master)
$ git add hello.txt
#将编辑好的文件添加到暂存区
Administrator@WIN-NNHMK0M7785 MINGW64 /e/test-git/test (master)
$ git commit -m "dongguo-test1 commit" hello.txt
[master 39e1db4] dongguo-test1 commit
1 file changed, 1 insertion(+), 1 deletion(-)
#将本地库的内容 push 到远程仓库
Administrator@WIN-NNHMK0M7785 MINGW64 /e/test-git/test (master)
$ git push origin master
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 287 bytes | 287.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 https://github.com/dongguo4812/test.git
888b61d..39e1db4 master -> master
6) 回到 dongguo482 的 GitHub 远程仓库中可以看到,最后一次是dongguo-test1提交的。
git pull 远程库地址别名 远程分支名
2)案例实操
dongguo4812将远程仓库对于分支最新内容拉下来后与当前本地分支直接合并
Administrator@WIN-NNHMK0M7785 MINGW64 /e/test (master)
$ git pull test master
remote: Enumerating objects: 8, done.
remote: Counting objects: 100% (8/8), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 6 (delta 2), reused 6 (delta 2), pack-reused 0
Unpacking objects: 100% (6/6), 484 bytes | 1024 bytes/s, done.
From https://github.com/dongguo4812/test
* branch master -> FETCH_HEAD
888b61d..4704e6a master -> test/master
Updating 888b61d..4704e6a
Fast-forward
hello.txt | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
跨团队协作
1) 将远程仓库的地址复制发给邀请跨团队协作的人,比如dongguo-test2
https://github.com/dongguo4812/test.git
2) 在dongguo-test2的 GitHub 账号里的地址栏复制收到的链接,然后点击 Fork 将项目叉到自己的本地仓库。叉成功后可以看到当前仓库信息。
hello git123456789
hello git111111111
hello git
hello git
hello git
hello git
hello git
hello git
1111111111 by dongguo-test1
2222222222 by dongguo-test2
4) 编辑完毕后,填写描述信息并点击左下角绿色按钮提交。
进入到聊天室,可以讨论代码相关内容。
填写对合并代码的描述,再次确认合并
在dongguo4812的远程库里就有了dongguo-test2提交的代码
拉取远程仓库代码到本地分支
-
Administrator@WIN-NNHMK0M7785 MINGW64 /e/test (master) $ git pull test master remote: Enumerating objects: 9, done. remote: Counting objects: 100% (9/9), done. remote: Compressing objects: 100% (5/5), done. remote: Total 7 (delta 2), reused 0 (delta 0), pack-reused 0 Unpacking objects: 100% (7/7), 1.93 KiB | 5.00 KiB/s, done. From https://github.com/dongguo4812/test * branch master -> FETCH_HEAD 4704e6a..787fdc5 master -> test/master Updating 4704e6a..787fdc5 Fast-forward hello.txt | 1 + 1 file changed, 1 insertion(+)
我们可以看到远程仓库中还有一个 SSH 的地址,因此我们也可以使用 SSH 进行访问。
git@github.com:dongguo4812/test.git
提示当前没有任何SSH keys,需要添加一个新的public key公钥或者使用HTTPS克隆远程仓库
具体操作如下:
1.删除.ssh 目录 (如果有)
C:\Users\Administrator目录下
删除.ssh目录
2.C:\Users\Administrator目录下,右键Git Bash Here
运行命令生成.ssh 秘钥目录[注意:这里-C 这个参数是大写的 C]
ssh-keygen -t rsa -C 账号邮箱
执行命令后,点击三次Enter键
Administrator@WIN-NNHMK0M7785 MINGW64 ~
$ ssh-keygen -t rsa -C 291320608@qq.com
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/Administrator/.ssh/id_rsa): Created directory '/c/Users/Administrator/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/Administrator/.ssh/id_rsa
Your public key has been saved in /c/Users/Administrator/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:yjvIkmczVDqEIEdGuylM0Jn2U7ZzJMbE1ExRyao/BNM 291320608@qq.com
The key's randomart image is:
+---[RSA 3072]----+
|.++o =o+o+.. |
|+.*. B + o |
|.=.o + = . |
|o .o+ * E |
|..o. + *S |
| . +.... |
| + ooo |
| o B ..o |
| + o.. . |
+----[SHA256]-----+
重新生成.ssh目录
公钥:
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC474HzcJvvqrrZm6CNGp4LQ/KuP5cJF88KHba8wCpUQ79Tl1d1rlAdwQ2d8kZpBiXyi8nQhkbSlUsa6iteQfm8xUrRVRiXmH/v4lwD0RpYohHEM4jkpJoB4MmAEEQcYclY+hkx2z6Yws8iMKyWDGvNcYA4KbnEzP6tIk+BKPHtILfLZcfoX3z0I8Hq44Xv8BFNUXPSGGz4ZbunNLM3tsLhLA+JIbMTv21P5UGJK/knAJS/Hp+fojE7IKTlrQ0L8RgfeeCZVKNyKhE+EQo7AEHa1dav+7yMGI4fTP3PFe6+hoS99B8f4+y8hO64IDnZJdmfzfkgesHg6joVkMzAP1rRznKVhT5cNczsyYed99ydATyS25VyEnahrxJ4gxNmhk2qoceKEbqs+8CDKHjervuHy6ouOGxs2NwPERiKmSJEoUJXChcaqCXZqRedpV6UAYEj2W7Lz02/QayTPnDOok4rEcuUcGzfGcF2vdksAGRU2dnP0a2xF2KJq5OHs1k0GEk= 291320608@qq.com
复制 id_rsa.pub 文件内容,登录 GitHub,点击用户头像→Settings→SSH and GPG keys
接下来再往远程仓库 push 东西的时候就可以使用 SSH 连接,不需要登录了。
dongguo4812修改hello.txt
Administrator@WIN-NNHMK0M7785 MINGW64 /e/test (master)
$ vim hello.txt
hello git123456789
hello git111111111
hello git
hello git
hello git
hello git
hello git
hello git
1111111111 by dongguo-test1
2222222222 by dongguo-test2
3333333333 by dongguo4812
:wq保存
将代码从本地库推送到远程库
Administrator@WIN-NNHMK0M7785 MINGW64 /e/test (master)
$ git push git@github.com:dongguo4812/test.git master
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 295 bytes | 147.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:dongguo4812/test.git
787fdc5..5d15ef0 master -> master
第一次会提示是否连接
The authenticity of host 'github.com (127.0.0.1)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com' (RSA) to the list of known hosts.