<p><span><span>最近在知乎学习Python,主要基于网络自动化相关模块和功能的学习,由于之前已学习相关Python基础和做过相关Python项目,故Python的基础在这不表,主要还是代码示例和功能实现,今天首先说明代码如何托管到Github和Gitee上。</span></span></p><p><span><span>下面实验都是基于 </span></span><span><span>@弈心</span></span><span><span> 大神《网络工程师的Python之路》中的实验基本是基于思科设备完成,参考 </span></span><span><span>@朱嘉盛</span></span><span><span> 老哥对弈心大神做华为系列的展示,边学边拓展而来。</span></span></p><p><span><span>在这里主要以Windows 11系统作为示例,说明相关整个学习线路涉及的工具。</span></span></p><h2><p><span><span>
</span></span></p>Git安装
</h2><p><span><span>官网下载:</span></span><span><span>https://git-scm.com/</span></span><span><span> 下载最小稳定安装版,做基础的Git安装,具体步骤不做基础解释,可自行谷歌。</span></span></p><h2><p><span><span>
</span></span></p>配置github和gitee
</h2><p><span><span>在此不再解释Github和Gitee是什么,不是小白文,还需要自行去谷歌,个人建议做技术还是需要注册</span></span><b><span>Github</span></b><span><span>或者</span></span><b><span>Gitee</span></b><span><span>,主要作为自己作品的展示和开源项目协作。</span></span></p><p><span><span>安装好后,打开Git Bash,如下图:</span></span></p><p/><div class="image-package"><img src="https://img.haomeiwen.com/i2084007/5b29a1b153140484.jpeg" img-data="{"format":"jpeg","size":18833,"height":537,"width":900}" class="uploaded-img" style="min-height:200px;min-width:200px;" width="auto" height="auto"/>
</div><p>
</p><p><span>开始配置Github,配置的前提是需要注册好Github账户,并了解Github的基础操作。</span></p><p><span><span>在这里Git访问其实就是非对称加密访问,有一个公钥和私钥之分,在自己本地生成公钥和私钥,把公钥复制并设置到Github上,形成互信,可以拉取Github仓库的项目,也可以上传自己写的项目和代码。接下来继续看操作:</span></span></p><span><span>neo@yanghua MINGW64 ~
mkdir .ssh
# 生成gitee和github 的 SSH Key
neo@yanghua MINGW64 ~/.ssh
ssh-keygen -t rsa -C "yanghuacba@outlook.com" -f "id_rsa_gitee"</span></span></p><span><span>Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in id_rsa_gitee
Your public key has been saved in id_rsa_gitee.pub
The key fingerprint is:
SHA256:8LZVMGwqSDFhLe3wMXuzj+C+AxAPksrQDj4MO8YhThU yanghuacba@outlook.com
The key's randomart image is:
+---[RSA 3072]----+
| o.E*= .o |
|Bo+.+.= oo |
|%=.= *.+ o . |
|+Oo o =o+ . |
|.... oSo. |
| . ...o |
| o ..o |
| o . . |
| .oo |
+----[SHA256]-----+</span></span><p><span>到此就完成Github和Gitee代码仓库的设置。</span>
</p><p><span><span style="font-size:16px">然后把生成的ssh </span></span><span style="font-size:16px">key 复制到Gitee,点击确定(操作类似上面的Github添加ssh)</span></p><p><span style="font-size:16px"/><span style="font-size:16px"/></p><div class="image-package"><img src="https://img.haomeiwen.com/i2084007/4fb2528c1cdfd3e7.jpeg" img-data="{"format":"jpeg","size":30758,"height":494,"width":1080}" class="uploaded-img" style="min-height:200px;min-width:200px;" width="auto" height="auto"/>
</div><p/><p><span style="font-size:16px"><span style="font-size:16px">编辑切换为居中添加Gitee公钥</span></span></p><p><span style="font-size:16px"><span style="font-size:16px"/></span></p><div class="image-package"><img src="https://img.haomeiwen.com/i2084007/7106abdd908a1e32.jpeg" img-data="{"format":"jpeg","size":30790,"height":462,"width":1080}" class="uploaded-img" style="min-height:200px;min-width:200px;" width="auto" height="auto"/>
</div><p/><p style="text-indent:0px"><strong>编辑切换为居中添加完成到此就完成Github和Gitee代码仓库的设置。</strong></p><h2><p><span><span>
</span></span></p>解决冲突
</h2><p><span><span>添加 config解决ssh冲突问题,因为目前.ssh中有两个密钥,在执行代码拉取和上传的过程中会出现ssh密钥冲突问题,需要新建config文件指定Github使用Github密钥,Gitee使用Gitee的密钥。</span></span></p><p><span><span>在.ssh文件夹路径下</span></span></p><p><span><span>执行命令:</span></span><b><span>vim config</span></b></p><p><b><span>config文件内容输入:</span></b></p><span><span># gitee
Host gitee.com
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_gitee
# github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_git</span></span><p><b><span/></b></p><h2><p><b><span>
</span></b></p>测试配置
</h2><p><b><span>测试是否配置成功,可以完成实现github和gitee和本机进行互相ssh通信。</span></b></p><span><span>ssh -T git@gitee.com
ssh -T git@github.com</span></span><p>如下图所示:
</p><div class="image-package"><img src="https://img.haomeiwen.com/i2084007/b20d6ccecbb5a8ea.jpeg" img-data="{"format":"jpeg","size":80556,"height":490,"width":1080}" class="uploaded-img" style="min-height:200px;min-width:200px;" width="auto" height="auto"/>
</div><p><span><span><span style="font-size:16px">编辑切换为居中完美解决问题</span><span style="font-size:16px">如出现图中所示,就代表完美通过。</span><span style="font-size:16px">测试代码push</span><span style="font-size:16px">在Github上新建一个仓库。</span></span></span></p><p><span><span><span style="font-size:16px"/></span></span></p><div class="image-package"><img src="https://img.haomeiwen.com/i2084007/e25b980d92844db4.jpeg" img-data="{"format":"jpeg","size":76277,"height":503,"width":829}" class="uploaded-img" style="min-height:200px;min-width:200px;" width="auto" height="auto"/>
</div><p/><p><span><span><span style="font-size:16px"><span style="font-size:16px">编辑切换为居中提交main</span><span style="font-size:16px">提交到main分支正常,在此完成说明下基础操作:</span></span></span></span></p><p><span><span>本地建立一个主分支main,本地主分支关联远程Github和Gitee,命令如下:</span></span></p><span><span># 关联gitee
git remote add gitee https://gitee.com/robotneo/DevNetOps.git
git push gitee main
# 关联github
git remote add github https://github.com/robotneo/DevNetOps.git
git push github main</span></span><p><span><span>当有文件更新需做如下操作:</span></span></p><span><span><span># 本地电脑有新文件添加或者改动,最好执行
git add .
git commit -m "first commit"
# </span>再<span>次执行推送代码到远程仓库
git push gitee
git push github</span></span></span><p><span><span>今天就到此结束,简单说明了把本地代码或项目推送保存到Github和Gitee代码仓库,后续继续说明相关工具和代码实例。</span></span></p><p>
</p>
网友评论