在git項(xiàng)目的根目錄下崖瞭,創(chuàng)建.gitignore
echo *.pyc >.gitignore //排除所有以.pyc為后綴的文件
比如python項(xiàng)目下,想排除venv 躏仇、 _pycache_ 2個(gè)目錄,可以寫成如下:
#cat .gitignore
venv/
__pycache__/
.* #忽略以.開(kāi)頭的文件或目錄
*.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)程倉(cāng)庫(kù)關(guān)聯(lián),并提交代碼
- 添加遠(yuǎn)程倉(cāng)庫(kù)地址:
#git remote add origin https://github.com/xxx/ws_tornado
#git remote -v //查看遠(yuǎn)程倉(cāng)庫(kù)地址
origin https://github.com/xxx/ws_tornado (fetch)
origin https://github.com/xxx/ws_tornado (push)
添加本地代碼
#git add . //添加需要提交的代碼
#git commit -m 'add all' //提交代碼到本地倉(cāng)庫(kù)提交代碼到遠(yuǎn)程倉(cāng)庫(kù)
# 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ò)躬拢,說(shuō)是當(dāng)前分支master沒(méi)有上游分支躲履,為了推送當(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)程倉(cāng)庫(kù)代碼拉到本地
- 最后提交代碼至遠(yuǎn)程倉(cāng)庫(kù):
# git push