DevNetOps之路--代碼上傳Github和Gitee

<p><span><span>最近在知乎學(xué)習(xí)Python,主要基于網(wǎng)絡(luò)自動(dòng)化相關(guān)模塊和功能的學(xué)習(xí),由于之前已學(xué)習(xí)相關(guān)Python基礎(chǔ)和做過相關(guān)Python項(xiàng)目,故Python的基礎(chǔ)在這不表,主要還是代碼示例和功能實(shí)現(xiàn),今天首先說明代碼如何托管到Github和Gitee上。</span></span></p><p><span><span>下面實(shí)驗(yàn)都是基于 </span></span><span><span>@弈心</span></span><span><span> 大神《網(wǎng)絡(luò)工程師的Python之路》中的實(shí)驗(yàn)基本是基于思科設(shè)備完成,參考 </span></span><span><span>@朱嘉盛</span></span><span><span> 老哥對弈心大神做華為系列的展示,邊學(xué)邊拓展而來。</span></span></p><p><span><span>在這里主要以Windows 11系統(tǒng)作為示例,說明相關(guān)整個(gè)學(xué)習(xí)線路涉及的工具。</span></span></p><h2><p><span><span>
</span></span></p>Git安裝
</h2><p><span><span>官網(wǎng)下載:</span></span><span><span>https://git-scm.com/</span></span><span><span> 下載最小穩(wěn)定安裝版,做基礎(chǔ)的Git安裝,具體步驟不做基礎(chǔ)解釋,可自行谷歌。</span></span></p><h2><p><span><span>
</span></span></p>配置github和gitee
</h2><p><span><span>在此不再解釋Github和Gitee是什么,不是小白文,還需要自行去谷歌,個(gè)人建議做技術(shù)還是需要注冊</span></span><b><span>Github</span></b><span><span>或者</span></span><b><span>Gitee</span></b><span><span>,主要作為自己作品的展示和開源項(xiàng)目協(xié)作。</span></span></p><p><span><span>安裝好后,打開Git Bash,如下圖:</span></span></p><p/><div class="image-package"><img src="https://upload-images.jianshu.io/upload_images/2084007-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的基礎(chǔ)操作。</span></p><p><span><span>在這里Git訪問其實(shí)就是非對稱加密訪問,有一個(gè)公鑰和私鑰之分,在自己本地生成公鑰和私鑰,把公鑰復(fù)制并設(shè)置到Github上,形成互信,可以拉取Github倉庫的項(xiàng)目,也可以上傳自己寫的項(xiàng)目和代碼。接下來繼續(xù)看操作:</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://upload-images.jianshu.io/upload_images/2084007-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://upload-images.jianshu.io/upload_images/2084007-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://upload-images.jianshu.io/upload_images/2084007-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://upload-images.jianshu.io/upload_images/2084007-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://upload-images.jianshu.io/upload_images/2084007-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://upload-images.jianshu.io/upload_images/2084007-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代碼倉庫的設(shè)置。</span>
</p><p><span><span style="font-size:16px">然后把生成的ssh </span></span><span style="font-size:16px">key 復(fù)制到Gitee,點(diǎn)擊確定(操作類似上面的Github添加ssh)</span></p><p><span style="font-size:16px"/><span style="font-size:16px"/></p><div class="image-package"><img src="https://upload-images.jianshu.io/upload_images/2084007-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://upload-images.jianshu.io/upload_images/2084007-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代碼倉庫的設(shè)置。</strong></p><h2><p><span><span>
</span></span></p>解決沖突
</h2><p><span><span>添加 config解決ssh沖突問題,因?yàn)槟壳?ssh中有兩個(gè)密鑰,在執(zhí)行代碼拉取和上傳的過程中會(huì)出現(xiàn)ssh密鑰沖突問題,需要新建config文件指定Github使用Github密鑰,Gitee使用Gitee的密鑰。</span></span></p><p><span><span>在.ssh文件夾路徑下</span></span></p><p><span><span>執(zhí)行命令:</span></span><b><span>vim config</span></b></p><p><b><span>config文件內(nèi)容輸入:</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>測試是否配置成功,可以完成實(shí)現(xiàn)github和gitee和本機(jī)進(jìn)行互相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://upload-images.jianshu.io/upload_images/2084007-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">如出現(xiàn)圖中所示,就代表完美通過。</span><span style="font-size:16px">測試代碼push</span><span style="font-size:16px">在Github上新建一個(gè)倉庫。</span></span></span></p><p><span><span><span style="font-size:16px"/></span></span></p><div class="image-package"><img src="https://upload-images.jianshu.io/upload_images/2084007-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分支正常,在此完成說明下基礎(chǔ)操作:</span></span></span></span></p><p><span><span>本地建立一個(gè)主分支main,本地主分支關(guān)聯(lián)遠(yuǎn)程Github和Gitee,命令如下:</span></span></p><span><span># 關(guān)聯(lián)gitee
git remote add gitee https://gitee.com/robotneo/DevNetOps.git
git push gitee main

# 關(guān)聯(lián)github
git remote add github https://github.com/robotneo/DevNetOps.git
git push github main</span></span><p><span><span>當(dāng)有文件更新需做如下操作:</span></span></p><span><span><span># 本地電腦有新文件添加或者改動(dòng),最好執(zhí)行
git add .
git commit -m "first commit"

# </span>再<span>次執(zhí)行推送代碼到遠(yuǎn)程倉庫
git push gitee
git push github</span></span></span><p><span><span>今天就到此結(jié)束,簡單說明了把本地代碼或項(xiàng)目推送保存到Github和Gitee代碼倉庫,后續(xù)繼續(xù)說明相關(guān)工具和代碼實(shí)例。</span></span></p><p>
</p>

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容