03 【远程库操作】
03 【远程库操作】
1.推送到远程库
1.1 创建github仓库
![image-20221006163144237](https://i0.hdslb.com/bfs/album/b05d10d6b37bb5f9f891ccf27830a317077d0488.png)
1.2 创建远程库地址别名
git remote -v
查看当前所有远程地址别名
git remote add [别名] [远程地址]
![image-20221006163319039](https://i0.hdslb.com/bfs/album/ab53875fc39c30f6ca095d24140d48074e126bde.png)
1.3 推送
git push [别名] [分支名]
![image-20221006163529176](https://i0.hdslb.com/bfs/album/dbe8022495ec04005cf86c6585d7b0ff9fdfbc7a.png)
![image-20221006163609629](https://i0.hdslb.com/bfs/album/e0cdbb08ab94f92488cea7364aff62c756f46af0.png)
2.加入团队并推送
2.1 克隆项目到本地
git clone 仓库地址
![image-20221006164655915](https://i0.hdslb.com/bfs/album/376b2d6c6b3ce3ec1884846b4612e4baaee887d6.png)
效果
完整的把远程库下载到本地
创建 origin 远程地址别名
初始化本地库
2.2 添加新成员加入团队
![image-20221006164837999](https://i0.hdslb.com/bfs/album/160e7bc082c369a8e1be8718e3ecc8641ba4feef.png)
![image-20221006164827658](https://i0.hdslb.com/bfs/album/0dd960325ff5f1a2577527586ff4a7831ea9447c.png)
![image-20221006165016543](https://i0.hdslb.com/bfs/album/48d28340170ded53a39b9dba38f7b59d804a5bef.png)
![image-20221006165030823](https://i0.hdslb.com/bfs/album/20ce4dbc52e106cacf6c718a37cf7e3276349e57.png)
2.3 推送更新到远程库
1.更改用户名
![image-20221006184421259](https://i0.hdslb.com/bfs/album/862b3ffad28ebcc8165429d329755c631d65d921.png)
2.修改文件并提交到本地库
![image-20221006170100066](https://i0.hdslb.com/bfs/album/2bf77de9ff6cad26fca0e4a7201823d9d5375629.png)
3.推送到远程库
![image-20221006184352833](https://i0.hdslb.com/bfs/album/af73530bbd73a051ef3385723d9097b22d0f58a2.png)
![image-20221006184333691](https://i0.hdslb.com/bfs/album/b3669e7d7b83667542ab915c56af73ec7b498259.png)
![image-20221006184344102](https://i0.hdslb.com/bfs/album/c26190ec9b82452be5a8183591a00cd667fb72e7.png)
3.远程库的 SSH 登录
在 Windows 10 系统中,凭据管理器
为我们记录了 GitHub 的 Windows 凭据,再次从终端经过 GitHub 写数据时,可自动保持登录状态。但其他 OS 不一定有这样的功能,因此不便于频繁地提交版本。 为此,可以使用 SSH 登录的方式访问远程库。
不同于 SSH 登录,使用 HTTPS 的方式可以在多个 GitHub 帐号间管理仓库。
![image-20220827231735884](https://i0.hdslb.com/bfs/album/0750d34afa912952dffe12e622d3246d79e29190.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://i0.hdslb.com/bfs/album/e3df81a1b0d5107c9ff362d889fddc622a9eec79.png)
3.2 查看 .ssh 目录下的文件
cd .ssh
cat id_rsa.pub
![image-20220827232355616](https://i0.hdslb.com/bfs/album/4a3cfe46bde89a158e7cbb3e69a94ad524d649f9.png)
将秘钥全部复制
3.3 在 GitHub 配置该 SSH 公钥
登录 GitHub 账户后,进入Settings中的SSH and GPG keys项执行配置:
![image-20220827232156887](https://i0.hdslb.com/bfs/album/a0d69e52601014165960be29aa4f0337c65e19ed.png)
![image-20220827232550986](https://i0.hdslb.com/bfs/album/824f07f439e1b196bb6149982e2a8e1daecbbe88.png)
3.4 更改在本地的远程库地址
使用git remote set-url <Alias4RemoteRepositoryAddress> <RemoteRepositoryAddress>
命令,将远程库地址由 HTTPS 方式切换为 SSH 方式:
![image-20220827233228262](https://i0.hdslb.com/bfs/album/47860f6ccebf9bc54d7ab03508c3bfb9691e840d.png)
也可以直接添加一个新的别名origin_ssh
![image-20221006192328180](https://i0.hdslb.com/bfs/album/8b98f488047eeb66f67cb46ed0a37be7a04b6210.png)
随后,即可使用 SSH 的方式免密读写 GitHub 的内容。
![image-20221006192705628](https://i0.hdslb.com/bfs/album/2b467d5e5a1589e6f173a8e13e0536317f579bea.png)
![image-20221006192719254](https://i0.hdslb.com/bfs/album/1f096bcdf229fcafe39fb35fe599fc304b204b02.png)
4.解决冲突
4.1 冲突的产生
冲突的产生:本地仓库与线上不一致时未git pull就开始编写代码 并且在编写完代码后试图git push
![image-20221006193046462](https://i0.hdslb.com/bfs/album/0795ecfec7107ef45a666b5a85c91cd0014dd448.png)
4.2 解决冲突
- 要点
- 如果不是基于 GitHub 远程库的最新版所做的修改,不能推送,必须先拉取。
- 拉取下来后如果进入冲突状态,则按照“分支冲突解决”操作解决即可。
- 首先我要在两个本地库更改
test2.txt
中的内容
dselegent
![image-20221006194022775](https://i0.hdslb.com/bfs/album/d97b6d67b7f1fb3d3438b623ed938e00cda0f894.png)
dselegent-gmail
![image-20221006194132829](https://i0.hdslb.com/bfs/album/b208a65e7079d88b836dd82e52c0f6f81893456e.png)
dselegent
推送到远程库
![image-20221006194251330](https://i0.hdslb.com/bfs/album/b85100c12247ffe3ea0d3fad8caf47574f9954ec.png)
![image-20221006194305353](https://i0.hdslb.com/bfs/album/bad1f1f4e88b1e815ce1145fe83f5d708a74355d.png)
![image-20221006194313397](https://i0.hdslb.com/bfs/album/c1501c43f0ebf34414cdc56e00a9fdf6f859470e.png)
dselegent-gmail
推送到远程库
![image-20221006194350085](https://i0.hdslb.com/bfs/album/1a220b056d94b03f7c2a871080a6e1b7b581d277.png)
- 在
dselegent-gmail
中使用git pull origin master
拉取最新的内容
pull=fetch+merge
git fetch [远程库地址别名]
git merge [远程库地址别名/远程分支名】
git pull [远程库地址别名] [远程分支名]
![image-20221006194801202](https://i0.hdslb.com/bfs/album/c4de3179c727ab71caec921e724fd5baa65b8f63.png)
![image-20221006194846829](https://i0.hdslb.com/bfs/album/cf371bc2ef278b043c064f40161721dbcc7efbad.png)
![image-20221006195232425](https://i0.hdslb.com/bfs/album/3ff0c93ee2d342482109fa4eb60d1467cf98aa2b.png)
dselegent-gmail
推送到远程库
![image-20221006195309995](https://i0.hdslb.com/bfs/album/bc7b0a1f2e151dc3a0d06c93be2a271958d6aacd.png)
![image-20221006195326732](https://i0.hdslb.com/bfs/album/2147294762cdc4d2786d1632133be9c505427061.png)
![image-20221006195334121](https://i0.hdslb.com/bfs/album/0d3ff32454438a26393341684896e6eb7ffe1d7d.png)
5.跨团队协作
这里先把
dselegent-gmail
退出团队用来做示例
- 点击右上角
fork
![image-20221006195537205](https://i0.hdslb.com/bfs/album/abedd3afcf209e0cd7d0e4afe2eee71801ca595d.png)
![image-20221006200645023](https://i0.hdslb.com/bfs/album/38e62a024e6ae0d7ba7f07f5e3924e5de45595c9.png)
- 克隆到本地
![image-20221006200507493](https://i0.hdslb.com/bfs/album/5bf2a4d9c3d73715a44267cf10a48ecc66cef573.png)
- 修改文件推送到远程库
![image-20221006200627526](https://i0.hdslb.com/bfs/album/97df763778fb3f52f33ed5326a9a5f50e464d833.png)
Pull Request
![image-20221006200928505](https://i0.hdslb.com/bfs/album/5dc9a0f07e66b63c8a4d2924060642ee4bfddc21.png)
![image-20221006200916957](https://i0.hdslb.com/bfs/album/258e00e80ea6b2a20aca8e5432e5ae95ef7beb45.png)
在
dselegent
中接收image-20221006201438135
![image-20221006201507387](https://i0.hdslb.com/bfs/album/201e4901e8105834e2cc0e88fc16cfe2c2c8eef2.png)
![image-20221006201542512](https://i0.hdslb.com/bfs/album/a5dda62ef328780fd512b719bc45bfdc2271caa3.png)