03 【远程库操作】
1.推送到远程库
1.1 创建github仓库
创建新仓库 (github.com)
![image-20221006163144237](https://img-blog.csdnimg.cn/img_convert/afeb787784f4789a9ada4f4b64d56d4e.png)
1.2 创建远程库地址别名
git remote -v 查看当前所有远程地址别名
git remote add [别名] [远程地址]
![image-20221006163319039](https://img-blog.csdnimg.cn/img_convert/6676122326eb32378f648f458bcd5e10.png)
1.3 推送
git push [别名] [分支名]
![image-20221006163529176](https://img-blog.csdnimg.cn/img_convert/2bb0144a95f2e75f8277192e3e762e17.png)
![image-20221006163609629](https://img-blog.csdnimg.cn/img_convert/925cfd1c4744f708f359bf7452cd16aa.png)
2.加入团队并推送
2.1 克隆项目到本地
git clone 仓库地址
![image-20221006164655915](https://img-blog.csdnimg.cn/img_convert/0d12bf6172c40fbae368a0101f095244.png)
效果
-
完整的把远程库下载到本地 -
创建 origin 远程地址别名 -
初始化本地库
2.2 添加新成员加入团队
![image-20221006164837999](https://img-blog.csdnimg.cn/img_convert/d9b547af69c8755272707846129042e4.png)
![image-20221006164827658](https://img-blog.csdnimg.cn/img_convert/73ce2cbfdf9b941f3e79fdca54f5ab6e.png)
![image-20221006165016543](https://img-blog.csdnimg.cn/img_convert/bde5bd21e08b91f1b43beabff385df1c.png)
![image-20221006165030823](https://img-blog.csdnimg.cn/img_convert/d2208ffac7dd764d5b5fc5fe90242644.png)
2.3 推送更新到远程库
1.更改用户名
![image-20221006184421259](https://img-blog.csdnimg.cn/img_convert/58261e51adf5cb3b2178def7e699b096.png)
2.修改文件并提交到本地库
![image-20221006170100066](https://img-blog.csdnimg.cn/img_convert/dbf8a21e52566ae0da9b3715691cd909.png)
3.推送到远程库
![image-20221006184352833](https://img-blog.csdnimg.cn/img_convert/6c1dbdb70e694b264be6b83c22714d1e.png)
![image-20221006184333691](https://img-blog.csdnimg.cn/img_convert/c83965ff0b0d3272e23bbb4814c797e8.png)
![image-20221006184344102](https://img-blog.csdnimg.cn/img_convert/782bc1856151f52531d60c2344201ba4.png)
3.远程库的 SSH 登录
在 Windows 10 系统中,凭据管理器 为我们记录了 GitHub 的 Windows 凭据,再次从终端经过 GitHub 写数据时,可自动保持登录状态。但其他 OS 不一定有这样的功能,因此不便于频繁地提交版本。 为此,可以使用 SSH 登录的方式访问远程库。
不同于 SSH 登录,使用 HTTPS 的方式可以在多个 GitHub 帐号间管理仓库。
![image-20220827231735884](https://img-blog.csdnimg.cn/img_convert/cafa98e8f7515c1e8a1f6e056b0fc2d9.png)
3.1 在本地 Home 目录生成 GitHub 公/私钥
cd ~ 进入当前用户的根目录
rm -rvf .ssh 删除.ssh 目录
使用命令ssh-keygen -t <KeyType> -C <Annotation|AccountEmailAddress> 在本地 Home 目录生成 GitHub 公/私钥。
ssh-keygen -t rsa -C dselgent@qq.com
![image-20220827232018116](https://img-blog.csdnimg.cn/img_convert/1f4945624c78329682e9a3d13d235cff.png)
3.2 查看 .ssh 目录下的文件
cd .ssh
cat id_rsa.pub
![image-20220827232355616](https://img-blog.csdnimg.cn/img_convert/2dedfde69851916be903662d09e3e855.png)
将秘钥全部复制
3.3 在 GitHub 配置该 SSH 公钥
登录 GitHub 账户后,进入Settings中的SSH and GPG keys项执行配置:
![image-20220827232156887](https://img-blog.csdnimg.cn/img_convert/4cc0d810640b0afa2d2ea26927d7144b.png)
![image-20220827232550986](https://img-blog.csdnimg.cn/img_convert/b4e516fc417a8effb8226692f002b9b2.png)
3.4 更改在本地的远程库地址
使用git remote set-url <Alias4RemoteRepositoryAddress> <RemoteRepositoryAddress> 命令,将远程库地址由 HTTPS 方式切换为 SSH 方式:
![image-20220827233228262](https://img-blog.csdnimg.cn/img_convert/a0a5ad9b5312394631a3c98cb28dc895.png)
也可以直接添加一个新的别名origin_ssh
![image-20221006192328180](https://img-blog.csdnimg.cn/img_convert/1258ded7bd8dd0e59a3eee1c0fd6978c.png)
随后,即可使用 SSH 的方式免密读写 GitHub 的内容。
![image-20221006192705628](https://img-blog.csdnimg.cn/img_convert/3d1de08f91d59f3f6d5cdb7ca786abd9.png)
![image-20221006192719254](https://img-blog.csdnimg.cn/img_convert/c81127a630e603f24b30589c85eb0440.png)
4.解决冲突
4.1 冲突的产生
冲突的产生:本地仓库与线上不一致时未git pull就开始编写代码 并且在编写完代码后试图git push
![image-20221006193046462](https://img-blog.csdnimg.cn/img_convert/e6f8e3331074378de665cde2559d6031.png)
4.2 解决冲突
- 要点
- 如果不是基于 GitHub 远程库的最新版所做的修改,不能推送,必须先拉取。
- 拉取下来后如果进入冲突状态,则按照“分支冲突解决”操作解决即可。
- 首先我要在两个本地库更改
test2.txt 中的内容
dselegent
![image-20221006194022775](https://img-blog.csdnimg.cn/img_convert/fad25aac8f4bd029df5117edb19cb4bd.png)
dselegent-gmail
![image-20221006194132829](https://img-blog.csdnimg.cn/img_convert/d0c6c4d08b40348abea5b87af4c4bc13.png)
dselegent 推送到远程库
![image-20221006194251330](https://img-blog.csdnimg.cn/img_convert/bb4b583cb4ed601c4432172652b89e75.png)
![image-20221006194305353](https://img-blog.csdnimg.cn/img_convert/609dab6ae1d756b3d49b689167ea4df3.png)
![image-20221006194313397](https://img-blog.csdnimg.cn/img_convert/102a36e5cd05cec3b4be1e61abd29ee3.png)
dselegent-gmail 推送到远程库
![image-20221006194350085](https://img-blog.csdnimg.cn/img_convert/9b87a2fba788170b889f0ca4ac5f655a.png)
- 在
dselegent-gmail 中使用git pull origin master 拉取最新的内容
pull=fetch+merge
git fetch [远程库地址别名]
git merge [远程库地址别名/远程分支名】
git pull [远程库地址别名] [远程分支名]
![image-20221006194801202](https://img-blog.csdnimg.cn/img_convert/c55708d966312885e104bb1f8ed2b5e7.png)
![image-20221006194846829](https://img-blog.csdnimg.cn/img_convert/6506738f39e17154f4ea79e54e5e1087.png)
![image-20221006195232425](https://img-blog.csdnimg.cn/img_convert/ae77f1df0c8d98daf18ad43c328f530e.png)
dselegent-gmail 推送到远程库
![image-20221006195309995](https://img-blog.csdnimg.cn/img_convert/715d740b7918e931f421a88809006c44.png)
![image-20221006195326732](https://img-blog.csdnimg.cn/img_convert/5237af6d87e6cd8528d7f454dd172bc0.png)
![image-20221006195334121](https://img-blog.csdnimg.cn/img_convert/1db426b82f29e6d8b435b4e556991c44.png)
5.跨团队协作
这里先把dselegent-gmail 退出团队用来做示例
- 点击右上角
fork
![image-20221006195537205](https://img-blog.csdnimg.cn/img_convert/37d02bd7d02afc5a8663275444f8ce21.png)
![image-20221006200645023](https://img-blog.csdnimg.cn/img_convert/823c2d54200699cf5867ea63bed2b547.png)
- 克隆到本地
![image-20221006200507493](https://img-blog.csdnimg.cn/img_convert/7fab68f9efd5a18ba88115eae4c36125.png)
- 修改文件推送到远程库
![image-20221006200627526](https://img-blog.csdnimg.cn/img_convert/e7cbef34bbf26a175b76f40155477a4c.png)
Pull Request
![image-20221006200928505](https://img-blog.csdnimg.cn/img_convert/93e956a78740162fb03facc719c67bb2.png)
![image-20221006200916957](https://img-blog.csdnimg.cn/img_convert/f6ef76a057e8422e3e7adb3dabcf30bf.png)
-
在dselegent 中接收 ![image-20221006201438135](https://img-blog.csdnimg.cn/img_convert/146bcc2a354b0868423887784d170b2b.png)
![image-20221006201507387](https://img-blog.csdnimg.cn/img_convert/301537014044c797248342db6156a28d.png)
![image-20221006201542512](https://img-blog.csdnimg.cn/img_convert/3df79a8ec33e4878bd9c1e107635b42e.png)
|