git上傳程式碼到github入門學習和相關錯誤彙總
git和github作為版本控制的角色在專案開發中起到了重要的作用,今天我們就來學習如何使用git上傳程式碼到github以及在操作中可能出現的問題,歡迎大家指正!
一,註冊github賬號
1,瀏覽器進入github:ofollow,noindex">https://github.com/
2,註冊好以後New repository,建倉庫
3,填寫倉庫名稱,可以選擇公開或私密,私密不免費,最好把Initialize this repository with a README勾上
4,到此倉庫已經建好。
二,新增(上傳)檔案到倉庫
1,上傳單個檔案
上傳單個檔案比較簡單,之間在倉庫介面點選Create new file——>輸入檔名(如果需要新增資料夾,可以輸入資料夾名稱之後,按下/ 就會自動分隔為資料夾路徑了)——>輸入檔案內容,或描述——>選擇commit new file,檔案建立完畢
2,上傳多個檔案(重點)
1)首先下載git:https://gitforwindows.org/
2)安裝後,右鍵桌面會有以下兩個選項:
*Git GUI here
*Git Bash here
我們選擇Git Bash here這項
3)在介面輸入ssh-keygen -t rsa -C"[email protected]"
目的是為了配置ssh,後面的[email protected]改為你的郵箱,就是github上註冊的那個郵箱,然後一路回車三次。不出意外ssh key已經生成成功
4),進入本地磁碟,填寫key到github
4-1,電腦開啟資料夾C:\Users\使用者名稱\.ssh
4-2,可以看到生成了兩個檔案id_rsa和id_rsa.pub
4-3,記事本開啟id_rsa.pub,複製裡面的內容。
4-4,瀏覽器回到github,點選右上角個人頭像向下三角形狀選擇setting-->ssh and GPG keys-->new ssh key
4-5,將複製的內容填入key中
title :自己填一個任意的
key :將剛才複製的所有內容複製到裡面
5)驗證剛剛填入的key
在git的終端輸入 ssh -T [email protected],回車後,輸入yes,會提示驗證成功
6)配置全域性的user和email
在git終端輸入:
git config --global user.name"your name"(your name換成你註冊github時的使用者名稱)
git config --global user.email"[email protected]"(email換成你註冊github的郵箱)
7)建立本地與github倉庫的連線
在git終端輸入:
testgit remote add origin [email protected]:yourName/yourRepo.git(把 your name 換成你註冊github時填寫的名字,把yourRepo 換成在github建立的倉庫的名字)
8)進入需要上傳程式碼的資料夾,例如:
在git終端輸入:cd c:/zsybim
9) 上傳檔案
在git終端輸入:git add ./ (上傳該資料夾下面的所有的檔案)
上傳單個檔案要指定路徑,如:git add ./read.txt
10) 提交檔案
在git終端輸入:git commit -m"commit"
11)本地倉庫推送到伺服器
在git終端輸入:git push origin master
12)最後檢視檔案是否上傳成功
進入github介面找到對應的倉庫,檢視剛剛上傳的檔案是否存在
最後多個檔案上傳就可以了
三,之後操作
以後上傳檔案就不用這麼麻煩了,因為準備工作都做好了,以後只需要下面的步驟就行:
7)建立本地與github倉庫的連線
在git終端輸入:
testgit remote add origin [email protected]:yourName/yourRepo.git(把 your name 換成你註冊github時填寫的名字,把yourRepo 換成在github建立的倉庫的名字)
8)進入需要上傳程式碼的資料夾,例如:
在git終端輸入:cd c:/zsybim
9) 上傳檔案
在git終端輸入:git add ./ (上傳該資料夾下面的所有的檔案)
上傳單個檔案要指定路徑,如:git add ./read.txt
10) 提交檔案
在git終端輸入:git commit -m"commit"
11)本地倉庫推送到伺服器
在git終端輸入:git push origin master
四,錯誤集錦
其實在操作過程中還是會有很多錯誤出現的,對於剛剛上手的朋友來說很多問題是不知道怎麼解決的,這裡羅列出了我在操作過程中出現的問題錯誤給大家參考:
1,輸入:testgit remote add origin [email protected]:yourName/yourRepo.git 時
報錯:fatal: Not a git repository (or any of the parent directories): .git
解決辦法:輸入 git init ,初始化一個本地倉庫
2,輸入:testgit remote add origin [email protected]:yourName/yourRepo.git 時
報錯:fatal: remote origin already exists.
解決辦法:1. 刪除Git倉庫中的origin資訊:git remote rm origin
2. 重新新增Git倉庫中的origin資訊
3,輸入:git push origin master 時
報錯:fatal: 'origin' does not appear to be a git repository
fatal: Could not read from remote repository.
Please make sure you have the correct access rights and the repository exists.
解決辦法:重新輸入一次:git remote add origin [email protected]:yourusername/test.git
4,輸入:git push origin master 時
報錯:fatal: remote error:
[email protected]/myarea is not a valid repository name Email [email protected] for help
解決辦法:使用git remote rm origin 然後再使用上傳命令
5,輸入:git push origin master 時
報錯:To [email protected]:yangzhi/hello.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to '[email protected]:yangzhi/hello.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushin
hint: to the same ref. You may want to first merge the remote changes (e.g.
hint: 'git pull') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
解決辦法:出現這個問題是因為github中的README.md檔案不在原生代碼目錄中。先進行程式碼合併 git pull --rebase origin master 再執行 git push origin master
原創作者:夢凌小樣
作品連結:https://www.jianshu.com/p/d46232b6c68d 【原創不易,轉載請註明出處,感謝理解】
一位愛生活,愛創作,愛分享,愛自己的90後女程式員一枚,記錄工作中的點點滴滴,一起學習,共同進步,期待能和優秀的您交上朋友