美文网首页
DevNetOps之路--代码上传Github和Gitee

DevNetOps之路--代码上传Github和Gitee

作者: 网络小斐 | 来源:发表于2022-07-04 11:48 被阅读0次

    <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 ~
    pwd<br/>/c/Users/neo<br/><br/># 新建.ssh目录<br/>neo@yanghua MINGW64 ~<br/> mkdir .ssh

    # 生成gitee和github 的 SSH Key
    neo@yanghua MINGW64 ~/.ssh
    ssh-keygen -t rsa -C "yanghuacba@outlook.com" -f "id_rsa_github"<br/>Generating public/private rsa key pair.<br/>Enter passphrase (empty for no passphrase):<br/>Enter same passphrase again:<br/>Your identification has been saved in id_rsa_github<br/>Your public key has been saved in id_rsa_github.pub<br/>The key fingerprint is:<br/>SHA256:p331rJF7emShsJ81wOCk9yy+Zt9wKCzROqE6Xj4RcWM yanghuacba@outlook.com<br/>The key's randomart image is:<br/>+---[RSA 3072]----+<br/>| |<br/>| . E o |<br/>| + = o |<br/>| . ..+ o . |<br/>| Soo.= + .|<br/>| ..+=o =.*o|<br/>| oo+oo+o=++|<br/>| .+. o=.o++o|<br/>| .o... o.o.+= |<br/>+----[SHA256]-----+</span></span><p><span><span>如下图所示:</span></span></p><div class="image-package"><img src="https://img.haomeiwen.com/i2084007/f7ba535d187a75aa.jpeg" img-data="{&quot;format&quot;:&quot;jpeg&quot;,&quot;size&quot;:53195,&quot;height&quot;:473,&quot;width&quot;:900}" class="uploaded-img" style="min-height:200px;min-width:200px;" width="auto" height="auto"/><br/></div><p><span><span/></span></p><p><b>查看生成的的公钥:</b><b>cat id_rsa_github.pub</b></p><div class="image-package"><img src="https://img.haomeiwen.com/i2084007/b3ad5a8a77f6e378.jpeg" img-data="{&quot;format&quot;:&quot;jpeg&quot;,&quot;size&quot;:65625,&quot;height&quot;:263,&quot;width&quot;:900}" class="uploaded-img" style="min-height:200px;min-width:200px;" width="auto" height="auto"/><br/></div><p><b/></p><p><b>拷贝ssh-rsa开头的ssh key,然后到github上添加ssh key</b></p><div class="image-package"><img src="https://img.haomeiwen.com/i2084007/094a60a5f3b857a9.jpeg" img-data="{&quot;format&quot;:&quot;jpeg&quot;,&quot;size&quot;:38467,&quot;height&quot;:442,&quot;width&quot;:1080}" class="uploaded-img" style="min-height:200px;min-width:200px;" width="auto" height="auto"/><br/></div><p><b><span><span style="font-size:16px">编辑切换为居中Github</span></span></b></p><p><b><span><span style="font-size:16px"/></span></b></p><p><b><div class="image-package"><img src="https://img.haomeiwen.com/i2084007/e9557a1c6c0f8fba.jpeg" img-data="{&quot;format&quot;:&quot;jpeg&quot;,&quot;size&quot;:38702,&quot;height&quot;:408,&quot;width&quot;:1080}" class="uploaded-img" style="min-height:200px;min-width:200px;" width="auto" height="auto"/><br/></div></b></p><p/><p><b><span><span style="font-size:16px"><span style="font-size:16px">编辑切换为居中公钥拷贝到Github上</span></span></span></b></p><p><b><span><span style="font-size:16px"><span style="font-size:16px"/></span></span></b></p><p><b><div class="image-package"><img src="https://img.haomeiwen.com/i2084007/b2fc348f66d956fa.jpeg" img-data="{&quot;format&quot;:&quot;jpeg&quot;,&quot;size&quot;:23937,&quot;height&quot;:291,&quot;width&quot;:1080}" class="uploaded-img" style="min-height:200px;min-width:200px;" width="auto" height="auto"/><br/></div></b></p><p/><p><b><span><span style="font-size:16px"><span style="font-size:16px"><span style="font-size:16px">编辑切换为居中添加完成</span><span style="font-size:16px">配置Gitee的 ssh key</span></span></span></span></b></p><p><b><span><span style="font-size:16px"><span style="font-size:16px"><span style="font-size:16px"/></span></span></span></b></p><p><b><div class="image-package"><img src="https://img.haomeiwen.com/i2084007/e963d155f735bb1c.jpeg" img-data="{&quot;format&quot;:&quot;jpeg&quot;,&quot;size&quot;:56524,&quot;height&quot;:506,&quot;width&quot;:882}" class="uploaded-img" style="min-height:200px;min-width:200px;" width="auto" height="auto"/><br/></div></b></p><p/><p><span><span><span style="font-size:15.3px">neo@yanghua MINGW64 ~/.ssh</span></span></span></p><p><span><span> 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>

    相关文章

      网友评论

          本文标题:DevNetOps之路--代码上传Github和Gitee

          本文链接:https://www.haomeiwen.com/subject/tcsdbrtx.html