05.git团队协作
一.团队内协作
二.跨团队协作
三.GitHub操作
GitHub 网址:https://github.com/
Ps:全球最大同性交友网站,技术宅男的天堂,新世界的大门,你还在等什么?
账号 姓名 验证邮箱
atguiguyueyue 岳不群 atguiguyueyue@aliyun.com
atguigulinghuchong 令狐冲 atguigulinghuchong@163.com
atguigudongfang1 东方不败 atguigudongfang@163.com
注:此三个账号为讲师使用账号,同学请自行注册,然后三个同学为一组进行团队协作!
3.1 新建远程仓库
3.2 远程仓库操作
命令名称 | 作用 |
---|---|
git remote -v | 查看当前所有远程地址别名 |
git remote add 别名 远程地址 | 起别名 |
git remote rm 别名 | git删除远程地址 |
git push 别名 分支 | 推送本地分支上的内容到远程仓库 |
git clone 远程地址 | 将远程仓库的内容克隆到本地 |
git pull 远程库地址别名 远程分支名 | 将远程仓库对于分支最新内容拉下来后与当前本地分支直接合并 |
3.2.1 创建远程仓库别名
3.2.1.1 基本语法
git remote -v 查看当前所有远程地址别名
git remote add 别名 远程地址
3.2.1.2 实操
Jason@JASON MINGW64 ~/Desktop/GitTest (master)
$ git remote -v
Jason@JASON MINGW64 ~/Desktop/GitTest (master)
$ git remote add git-demo https://github.com/Jason-Vue/git-demo.git
Jason@JASON MINGW64 ~/Desktop/GitTest (master)
$ git remote -v
git-demo https://github.com/Jason-Vue/git-demo.git (fetch)
git-demo https://github.com/Jason-Vue/git-demo.git (push)
Jason@JASON MINGW64 ~/Desktop/GitTest (master)
3.2.2 推送本地分支到远程仓库
3.2.2.1 基本语法
git push 别名 分支
3.2.3 ==新版本的GitHub远程推送新方式(需要token)==
原因:自2021年8月13日起,github不再支持使用密码push的方式
3.2.3.1 使用Personal access token
首先,需要获取token
- 点击你的GitHub头像 -> 设置 -> 开发者设置 -> Personal access tokens -> Generate new token
- 生成token
3.复制token
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
4.使用token进行push、pull、clone等操作(pull和clone等操作原理同push,只需替换push为pull或其他相应的命令即可)
使用token的方式其实原理在于将原来明文密码换为token,说白了就是token>=password,之所以我这里写了>号,是因为token的功能远大于原来的password,相比password,token具有很多其没有的用法。
我将使用token的方法进行了细分,以满足不同的使用要求。请各位根据自己的使用情况进行选择
==token法一:直接push==
此方法每次push都需要输一遍token,很是费劲啊
# git push https://你的token@你的仓库链接,我这里是我的仓库链接你要改成你的
git push https://你的token@github.com/sober-orange/study.git
==token法二:修改remote别名==
这种方式在push的时候直接指明别名就可
如果你已经设置过remote别名,使用如下命令
# 我这里的别名是origin
# git remote set-url 你的remote别名 https://你的token@你的仓库地址
git remote set-url origin https://你的token@github.com/sober-orange/study.git
# 提交代码
git push -u origin master
如果你未设置过别名,使用如下命令添加别名
# git remote add 别名 https://你的token@你的仓库地址
git remote add origin https://你的token@github.com/sober-orange/study.git
# 提交代码
git push -u origin master
==token法三:使用Git Credential Manager Core (GCM Core) 记住token==
git push
Username: 你的用户名
Password: 你的token
# 记住token
git config credential.helper store
==toekn法四:使用Windows的凭据管理器==
- 打开凭据管理器 -> windows凭据
- 找到“git:https://github.com”的条目,编辑它
-
用token替换你以前的密码
img
3.2.3 token实操
Jason@JASON MINGW64 ~/Desktop/GitTest (master)
$ git remote add git-test01 https://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@github.com/Jason-Vue/git-demo.git
Jason@JASON MINGW64 ~/Desktop/GitTest (master)
$ git remote -v
git-demo https://github.com/Jason-Vue/git-demo.git (fetch)
git-demo https://github.com/Jason-Vue/git-demo.git (push)
git-test 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@https://github.com/Jason-Vue/git-demo.git (fetch)
git-test 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@https://github.com/Jason-Vue/git-demo.git (push)
git-test01 https://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@github.com/Jason-Vue/git-demo.git (fetch)
git-test01 https://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@github.com/Jason-Vue/git-demo.git (push)
Jason@JASON MINGW64 ~/Desktop/GitTest (master)
$ git push git-test01 master
fatal: unable to access 'https://github.com/Jason-Vue/git-demo.git/': OpenSSL SSL_read: Connection was reset, errno 10054
Jason@JASON MINGW64 ~/Desktop/GitTest (master)
$ git push git-test01 master
Enumerating objects: 21, done.
Counting objects: 100% (21/21), done.
Delta compression using up to 4 threads
Compressing objects: 100% (14/14), done.
Writing objects: 100% (21/21), 1.72 KiB | 160.00 KiB/s, done.
Total 21 (delta 6), reused 0 (delta 0)
remote: Resolving deltas: 100% (6/6), done.
To https://github.com/Jason-Vue/git-demo.git
* [new branch] master -> master
3.2.4 Git删除网络远程地址
git remote rm 别名
Jason@JASON MINGW64 ~/Desktop/GitTest (master)
$ git remote -v
git-demo https://github.com/Jason-Vue/git-demo.git (fetch)
git-demo https://github.com/Jason-Vue/git-demo.git (push)
git-test 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@https://github.com/Jason-Vue/git-demo.git (fetch)
git-test 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@https://github.com/Jason-Vue/git-demo.git (push)
git-test01 https://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@github.com/Jason-Vue/git-demo.git (fetch)
git-test01 https://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@github.com/Jason-Vue/git-demo.git (push)
Jason@JASON MINGW64 ~/Desktop/GitTest (master)
$ git remote rm git-demo
Jason@JASON MINGW64 ~/Desktop/GitTest (master)
$ git remote -v
git-test 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@https://github.com/Jason-Vue/git-demo.git (fetch)
git-test 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@https://github.com/Jason-Vue/git-demo.git (push)
git-test01 https://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@github.com/Jason-Vue/git-demo.git (fetch)
git-test01 https://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@github.com/Jason-Vue/git-demo.git (push)
Jason@JASON MINGW64 ~/Desktop/GitTest (master)
$ git remote rm git-test
Jason@JASON MINGW64 ~/Desktop/GitTest (master)
$ git remote -v
git-test01 https://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@github.com/Jason-Vue/git-demo.git (fetch)
git-test01 https://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@github.com/Jason-Vue/git-demo.git (push)
3.2.5 拉取远程库的内容
3.2.5.1 基本语法
git pull 远程库地址别名 分支
3.2.5.2 实操
Jason@JASON MINGW64 ~/Desktop/GitTest (master)
$ git pull git-test01 master
remote: Enumerating objects: 8, done.
remote: Counting objects: 100% (8/8), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 6 (delta 2), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (6/6), done.
From https://github.com/Jason-Vue/git-demo
* branch master -> FETCH_HEAD
379cbbf..7d1ed0a master -> git-test01/master
Updating 379cbbf..7d1ed0a
Fast-forward
hello.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
Jason@JASON MINGW64 ~/Desktop/GitTest (master)
$ cat hello.txt
hello world!hello Git!
hello world!hello Git!
hello world!hello Git!
hello world!hello Git!
hello world!hello Git!
hello world!hello Git! master分支
hello world!hello Git!
hello world!hello Git! hot-fix分支合并
我是岳不群在线更改的代码!!!
3.2.6 克隆远程仓库到本地仓库
3.2.6.1 基本语法
git clone 远程地址
3.2.6.2 实际操作
Jason@JASON MINGW64 ~/Desktop/git-lhc
$ git clone https://github.com/Jason-Vue/git-demo.git
Cloning into 'git-demo'...
remote: Enumerating objects: 27, done.
remote: Counting objects: 100% (27/27), done.
remote: Compressing objects: 100% (12/12), done.
remote: Total 27 (delta 8), reused 20 (delta 6), pack-reused 0
Unpacking objects: 100% (27/27), done.
Jason@JASON MINGW64 ~/Desktop/git-lhc
$ cd git-demo
Jason@JASON MINGW64 ~/Desktop/git-lhc/git-demo (master)
$ ll
total 1
-rw-r--r-- 1 Jason 197121 265 9月 9 09:45 hello.txt
Jason@JASON MINGW64 ~/Desktop/git-lhc/git-demo (master)
$ cat hello.txt
hello world!hello Git!
hello world!hello Git!
hello world!hello Git!
hello world!hello Git!
hello world!hello Git!
hello world!hello Git! master分支
hello world!hello Git!
hello world!hello Git! hot-fix分支合并
我是岳不群在线更改的代码!!!
Jason@JASON MINGW64 ~/Desktop/git-lhc/git-demo (master)
$ ls
hello.txt
Jason@JASON MINGW64 ~/Desktop/git-lhc/git-demo (master)
小结:clone 会做如下操作:
1、拉取代码。
2、初始化本地仓库。
3、创建别名
3.3 ==团队内协作==
3.3.1 选择邀请合作者
3.3.2 填入想要合作的人
3.3.3 复 制 地 址 并 通 过 微 信 钉 钉 等 方 式 发 送 给 该 用 户 , 复 制 内 容 如 下 :
https://github.com/atguiguyueyue/git-shTest/invitations
3.3.4 在atguigulinghuchong 这个账号中的地址 栏 复制 收到邀请的链接 ,点击接受邀请。
3.3.5 在成功之后可以在 atguigulinghuchong 这个账号上看到 git-Test的远程仓库
3.3.6 并令狐冲可以修改内容并 push
--编辑 clone 下来的文件
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/pro-linghuchong/git-shTest
(master)
$ vim hello.txt
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/pro-linghuchong/git-shTest
(master)
$ cat hello.txt
hello git! hello atguigu! 2222222222222
hello git! hello atguigu! 33333333333333
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu! 我是最帅的,比岳不群还帅
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu! master test
hello git! hello atguigu! hot-fix test
--将编辑好的文件添加到暂存区
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/pro-linghuchong/git-shTest
(master)
$ git add hello.txt
--将暂存区的文件上传到本地库
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/pro-linghuchong/git-shTest
(master)
$ git commit -m "lhc commit" hello.txt
[master 5dabe6b] lhc commit
1 file changed, 1 insertion(+), 1 deletion(-)
--将本地库的内容 push 到远程仓库
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/pro-linghuchong/git-shTest
(master)
$ git push origin master
Logon failed, use ctrl+c to cancel basic credential prompt.
Username for 'https://github.com': atguigulinghuchong
Counting objects: 3, done.
Delta compression using up to 12 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 309 bytes | 309.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/atguiguyueyue/git-shTest.git
7cb4d02..5dabe6b master -> master
3.3.7 回到 atguiguyueyue 的 的 GitHub 远程仓库中可以看到,最后一次是 lhc 提交的。
3.4 ==跨团队协作==
3.4.1 将远程仓库的地址复制发给邀请跨团队协作的人,比如东方不败
3.4.2 在东方不败的 GitHub 账号里的地址栏复制收到的链接,然后点击 Fork 将项目叉到自己的本地仓库。
叉入中…
叉成功后可以看到当前仓库信息。
3.4.3 东方不败就可以在线编辑叉取过来的文件。
3.4.4 编辑完毕后,填写描述信息并点击左下角绿色按钮提交。
3.4.5 接下来点击上方的 Pull 请求,并创建一个新的请求。
3.4.6 回到岳岳 GitHub 账号可以看到有一个 Pull request
进入到聊天室,可以讨论代码相关内容。
3.4.7 如果代码没有问题,可以点击 Merge pull reque 合并代码。
四.==SSH免密登录==
4.1 我们可以看到远程仓库中还有一个 SSH 的地址,因此我们也可以使用 SSH 进行访问。
这意味着这里没有生成SSH的key
4.2 生成SSH公钥和私钥
4.2.1 首先进入.ssh文件夹下,确保里面空空如也(如不为空,则会有下面的冲突)
ssh-keygen -t rsa -C wei1396981310@163.com(github邮箱地址)
注意:这里-C 这个参数是大写的 C
Jason@JASON MINGW64 ~/.ssh
$ ssh-keygen -t rsa -C wei1396981310@163.com
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/13969/.ssh/id_rsa):
/c/Users/13969/.ssh/id_rsa already exists.
Overwrite (y/n)?
Jason@JASON MINGW64 ~/.ssh
$
Jason@JASON MINGW64 ~/.ssh
$ ssh-keygen -t rsa -C wei1396981310@163.com
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/13969/.ssh/id_rsa):
/c/Users/13969/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/13969/.ssh/id_rsa.
Your public key has been saved in /c/Users/13969/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:A/eE61u01d4QTghPCW1bDMOEkHk4vjcIK2rghh6nCUI wei1396981310@163.com
The key's randomart image is:
+---[RSA 3072]----+
| .=o*=+ |
| =.o+=oo |
| ..oo..ooo |
| .o.+ .+ . |
| E oSoo . + |
|o . ..oooo . o |
|=o o . ..o. . .|
|+oB o |
|o= . |
+----[SHA256]-----+
4.2.2 查看公钥和私钥
Jason@JASON MINGW64 ~/.ssh
$ ll
total 10
-rw-r--r-- 1 Jason 197121 268 8月 17 19:37 config
-rw-r--r-- 1 Jason 197121 2610 9月 9 21:02 id_rsa
-rw-r--r-- 1 Jason 197121 575 9月 9 21:02 id_rsa.pub
-rw-r--r-- 1 Jason 197121 1117 7月 13 23:50 known_hosts
Jason@JASON MINGW64 ~/.ssh
$ cat id_rsa
-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAABlwAAAAdzc2gtcn
NhAAAAAwEAAQAAAYEA1ja8peDhbtvP52n5k8jmSVmWEqdQoLSVBz7g2lhSkdaF7SCaPpmF
f5/t++F+KlYijQ8Sd6PWxhooFixn7qlWMSo54GkBWTSG7Fgrhg58anRP1Cb77/WIh7UFi9
SFfvhVo87WU1r6ga/qLVr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