在git项目的根目录下,创建.gitignore
echo *.pyc >.gitignore //排除所有以.pyc为后缀的文件
比如python项目下,想排除venv 、 _pycache_ 2个目录,可以写成如下:
#cat .gitignore
venv/
__pycache__/
.* #忽略以.开头的文件或目录
*.a #忽略所以以.a为扩展名的文件
doc/*.txt #忽略文件比如doc/1.txt,但是文件如doc/server/a.txt 不忽略
目录排除一定需要在目录名后面加反斜杠 / ,不然会当成单文件处理。
# git add .
# git commit -m"添加gitignore忽略文件"
若要排除.gitignore文件本身,需要修改.git/info/exclude 文件,添加如下:
.gitignore
Case 1 :
将本地代码与远程仓库关联,并提交代码
- 添加远程仓库地址:
#git remote add origin https://github.com/xxx/ws_tornado
#git remote -v //查看远程仓库地址
origin https://github.com/xxx/ws_tornado (fetch)
origin https://github.com/xxx/ws_tornado (push)
添加本地代码
#git add . //添加需要提交的代码
#git commit -m 'add all' //提交代码到本地仓库提交代码到远程仓库
# 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
出现以上报错,说是当前分支master没有上游分支,为了推送当前分支并与远程分支建立关联,需要执行:
# git push --set-upstream origin master
指向完之后,由出现如下报错:
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
【若这一步报错“fatal: refusing to merge unrelated histories”,
则执行git pull origin master --allow-unrelated-histories】
建立与远程分支的关联,并将远程仓库代码拉到本地
- 最后提交代码至远程仓库:
# git push