git与远程仓库关联,并提交代码

在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 :

将本地代码与远程仓库关联,并提交代码

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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。