git上傳至github
上傳前先設定帳號密碼
git config --global user.email "your@example.com"
git config --global user.name "your name"
移到目標資料夾後,將git資料夾功能加入(會創一個.git資料夾)
git init
然後先看下圖釐清大致的流程
這張圖本章節先走上面的流程
使用git add . 將檔案加入索引
(後面的.表示整個資料夾全部都加入)
git add .
使用git commit -m . 將檔案加入索引
git commit -m "first commit"
建立分支
這裡的"main"是上傳到github上面時的分支
如果之前已經有同樣名字的在github上,會出現衝突,這個狀況push的時候會細談
git branch -M main
與github節點連接
github創一個出來之後複製貼過來
git remote add origin https://github.com/redmaple/example (網址為範例)
如果你已經上傳過,用不同電腦上傳時可能會遇到
(.venv) redmaple-suisei@redmaplesuisei:~/Desktop/order$ git remote add origin https://github.com/redmaple/example
error: remote origin already exists.
這是代表你的"origin被重複使用",origin通常是預設remote pointer
你可以試著改一個新的remote pointer,我這裡改一個test
git remote rename origin try
然後你可以打git remote -v 做確認,是否有更改完成
(.venv) redmaple-suisei@redmaplesuisei:~/Desktop/order$ git remote -v
origin https://github.com/redmaple/example (fetch)
origin https://github.com/redmaple/example (push)
test https://github.com/redmaple/example (fetch)
test https://github.com/redmaple/example (push)
將檔案推上遠端數據庫 |git push
git push -u origin main
可能會遇到的問題:main已經佔了位置(上傳過)就會出現,就算不是在同一個專案裡的git init,也會影響到。
比如a專案已經使用main,b專案也使用main去push時那就會出現這個問題。
To https://github.com/redmaple/example
! [rejected] main -> main (fetch first)
error: failed to push some refs to 'https://github.com/redmaple/example
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.
這種狀況有兩種作法
1.改另外一個名字傳上去
這種作法不只是解決問題的方法,想特別保留某個進度,但又怕主進度覆蓋掉舊的紀錄可以使用
我這裡直接改一個新名稱叫order-function,其餘照舊,舊能區分出main跟你這筆是不會互相影響跟覆蓋的問題
git branch -M order-function
之後照步驟使用就能解決
git push -u origin "order-function" (改成這個)
2.強制覆蓋掉 | 將 git push -u 改成-f
這種作法只在很確定可能是在另外一台電腦上傳也確定程式碼沒問題時使用,但同時要更新main的內容時使用
這種上傳法蓋掉就不能復原了,請注意
git push -f origin main
Visit the link for more information:
References