git命令行添加本地倉庫到github網站以及刪除、更新操作

1.在github上建好倉庫

2.sudo apt-get install git

3.生成非對稱祕鑰,將公鑰放到github賬戶設置中

4.驗證能否成功連接到github

5.設置username和email

以上內容在 ubuntu上傳代碼文件到github 博客中寫得很明白。

2019.12.13添:
下面將以上內容的代碼部分寫下來(畢竟自己經常忘):
生成密鑰命令: ssh-keygen -t rsa -C "youremail@dns"
複製.ssh文件夾中的id_rsa.pub文件,粘貼至github網站Account Setting-SSH Keys-Add SSH
最後命令: ssh -T [email protected]
若github不認識你的話,以下命令作爲登錄的備用選項(需要的時候git也會提醒你):
git config --global user.name "name"
git config --global user.email email@dns

若你的github賬號永遠都是如此,則以上操作只用執行一次。

平常使用git的命令過程

刪除操作:

git clone yourRepositoryURL
cd yourRepository
git rm -r theDirectory/          (刪除目錄)
git rm theDirectory/theFile      (刪除文件)
git commit -m 'del'
git push origin master

參考鏈接:使用 Git 上傳、更新、刪除 GitHub 倉庫文件

遠端更新(將本地代碼更新至github服務器)操作:(延續刪除操作的風格)

git status
git add *
git commit -m update
git pull origin
git push origin master

參考鏈接:將本地代碼使用Git上傳/更新至Github,並從Github下載代碼

本地操作:

git pull origin

下面舉例說明:

git下載fork的一個Information-Maximizing-GAN項目

在github網站上上傳一個hello.py文件,開始進行本地更新

在本地創建world.py文件後上傳更新到github服務器中

已更新到github網站端

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

-----------------------------------以下是在掌握上述精簡命令前學習的git操作,只是用於個人備忘。---------------------------------------

下面的操作是要你每次添加本地代碼到遠程服務器中都要進行的。

6.接下來cd到本地倉庫目錄中,執行以下命令:

git init
git add .
git commit -m "your commit comment" (此行也可以寫git commit, 
不過命令回車之後會進入vi界面讓你寫comment,沒有像前面那條寫一個隨意的comment速度快)
git commit -a
git remote add yourRemoteName [email protected]:yourUserName/yourRepositoryName.git
git push yourRemoteName master

若以上最後一條操作出現如下報錯:

 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to '[email protected]:yourUserName/yourRepositoryName.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

執行以下命令:

git fetch yourRemoteName
git merge yourRemoteName/master
git push yourRemoteName master

若第二條命令出現報錯:
fatal: refusing to merge unrelated histories
執行git merge yourRemoteName/master --allow-unrelated-histories

以上應該可以實現本地倉庫上傳到github倉庫中。

第6條操作參考鏈接:

更多git使用命令

出現“因爲您當前分支的最新提交落後於其對應的遠程分支”等類似錯誤後操作(之前這個鏈接已經引用過)

fatal: refusing to merge unrelated histories報錯處理

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章