git與遠(yuǎn)程倉庫關(guān)聯(lián),并提交代碼

在git項(xiàng)目的根目錄下,創(chuàng)建.gitignore
echo *.pyc >.gitignore //排除所有以.pyc為后綴的文件
比如python項(xiàng)目下,想排除venv 、 _pycache_ 2個(gè)目錄,可以寫成如下:

#cat .gitignore 
venv/
__pycache__/
.*                 #忽略以.開頭的文件或目錄
*.a                #忽略所以以.a為擴(kuò)展名的文件
doc/*.txt          #忽略文件比如doc/1.txt,但是文件如doc/server/a.txt 不忽略

目錄排除一定需要在目錄名后面加反斜杠 / ,不然會(huì)當(dāng)成單文件處理。

# git add .
# git commit -m"添加gitignore忽略文件"

若要排除.gitignore文件本身,需要修改.git/info/exclude 文件,添加如下:

.gitignore

Case 1 :

將本地代碼與遠(yuǎn)程倉庫關(guān)聯(lián),并提交代碼

origin  https://github.com/xxx/ws_tornado (fetch)
origin  https://github.com/xxx/ws_tornado (push)
  • 添加本地代碼
    #git add . //添加需要提交的代碼
    #git commit -m 'add all' //提交代碼到本地倉庫

  • 提交代碼到遠(yuǎn)程倉庫
    # git push

fatal: The current branch master has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin master

出現(xiàn)以上報(bào)錯(cuò),說是當(dāng)前分支master沒有上游分支,為了推送當(dāng)前分支并與遠(yuǎn)程分支建立關(guān)聯(lián),需要執(zhí)行:
# git push --set-upstream origin master

指向完之后,由出現(xiàn)如下報(bào)錯(cuò):

To https://github.com/xxx/ws_tornado
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/xxx/ws_tornado'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

# git branch --set-upstream-to=origin/master master
# git pull
【若這一步報(bào)錯(cuò)“fatal: refusing to merge unrelated histories”,
則執(zhí)行g(shù)it pull origin master --allow-unrelated-histories】
建立與遠(yuǎn)程分支的關(guān)聯(lián),并將遠(yuǎn)程倉庫代碼拉到本地

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

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