<div style="text-align:right;"><h6>2023/3/20</h1></div> # <center>GitHub 學習筆記 (二) push to GitHub</center> ### <center> (二) 如何把現有的 repository push 到 git hub 上 </center> #### <center> Git - pushing an existing repository </center> --- ## **++前情提要/事前準備++** >距離上一次筆記居然已經過了一個月,要督促自己好好學習,不能3分鐘熱度QQ ### **++Step 1 - 在 Github 上開新專案++** #### **++新增一個 new repository++** 要把檔案上傳到github,首先要先在上面開一個專案 (repo)。請在Github主頁右上角點選「+」號,並選擇「New repository」:  這裡可以自己設定repository,也可以選擇要設定為**公開還是私人**的,非常方便! (2019年初開始,GitHub 免費版的也可以設定為私人了)  同一個頁面當中,也可以選擇是否直接在生成 repository 的時候**自動生成 README file**。  ### **++Step 2 - 初始化自己的Git Folder++** 進入自己要上傳的資料夾檔案目錄下: #### **++初始化 Git Repo++** ``` git init ``` 在這個步驟會在根目錄下建立一個隱藏的.git資料夾,讓 git 可以識別並且用它來儲存版本歷史等等資訊。 #### **++把檔案加入到git的暫存區++** ``` git add -A ``` 這裡的 '-A'代表 include all,會把所有根目錄下的資料夾都放到暫存區,如果有部分檔案沒有要上傳到 github 上,可以用 (如果同步要刪除 local 端的檔案就把 --cached 拿掉): ``` // Remove directory from Git but NOT local git rm -r --cached 'myFolder/myFile' git commit -m "Removed folder/file from repository" git push origin <your-git-branch> ``` 再來用 git status 確認: ``` git status ``` 可以發現上一步 add 的檔案因為還沒有commit所以會被提醒  #### **++Commit 加入的檔案++** ``` git commit -m 'push existing project' ``` "-m"代表 message,後面的字串可以用來表示這次的commit做了怎麼樣的更新,並且會出現在 GitHub 上,方便之後做確認,或是讓其他共同工作的夥伴知道自己做了怎麼樣的更新。 #### **++新增一個遠端的 server++** 到前一步為止,我們的操作都是在自己的電腦上,接下來要連接到遠端的 Git。首先要設定一個端節的節點,例如: ``` git remote add origin git@github.com:cjm-jamie/my-new-projects.git ``` 這裡記得要選擇 **ssh** 的網址,可以到這裡複製:  #### **++把檔案推上遠端的 Git 伺服器++** ``` git branch -M main git push -u origin main ``` 如果在 push 的時候出現版本不同,無法 push 的問題,可以嘗試在 "-u" 後用 "-f",代表覆蓋掉之前所有的檔案,用新的檔案取代,這樣就不會出現版本不同而無法上傳的問題,但也**要確認目前 GitHub 上的檔案是可以被取代的才能這樣做,否則可能會被追殺==+**。請謹慎使用! 這裡要注意一下,因為原先舊版的Git預設branch是 "master",新版則是 "main",這部分可以在GitHub上看到,所以如果遇到類似下方的error: ``` error: src refspec main does not match any error: failed to push some refs to 'github.com:cjm-jamie/2023_ML.git' ``` 可以先確認自己在哪一個 branch 底下: ``` git branch ``` 如果發現不對,可以嘗試以下方法解決: ``` git branch -M main ``` 再 push 到 origin 一次 ``` git push -u origin main # for first time, next time can only enter "git push" ``` #### **++第一次 push 全部步驟 All Together++** ``` git init git add -A git commit -m 'Added my project' git remote add origin git@github.com:cjm-jamie/my-new-project.git git status git pull git push -u origin main ``` #### Official recommendation of first commit ``` echo "# EE6620_computational_photography" >> README.md git init git add README.md git commit -m "first commit" git branch -M main git remote add origin git@github.com:cjm-jamie/EE6620_computational_photography.git git push -u origin main ``` #### 刪除 repository 1. 刪掉 github 上的 repo 2. 刪掉 local 的 .git folder/ LICENCE file - 參考[網址](https://franksios.medium.com/github-%E5%88%AA%E9%99%A4github%E4%B8%8A%E7%9A%84%E5%B0%88%E6%A1%88-a3218b1beafe) ### **++後記++** >這次在push的時候因為名字從master改到main,一直push不上去,卡了好久,但是最後成功解決還是很有成就感!接下來要開始學期中的忙碌了,希望接下來還能持續生產筆記! >Keep going! :)) #### 參考網址 1. https://docs.github.com/en 2. https://www.digitalocean.com/community/tutorials/how-to-push-an-existing-project-to-github 3. https://www.baeldung.com/java-git-src-refspec-match 4. https://gitbook.tw/chapters/github/push-to-github <hr> <div style="text-align:right;"><h6>撰於 2023/3/20</h1></div>
×
Sign in
Email
Password
Forgot password
or
Sign in via Google
Sign in via Facebook
Sign in via X(Twitter)
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
Continue with a different method
New to HackMD?
Sign up
By signing in, you agree to our
terms of service
.