git init #在当前目录创建一个新的代码库
git add README.md #添加readme.md文件
git commit -m '此处添加上传内容书名' #提交暂停区到仓库
##开始添加远程仓库地址
git remote add origin 远程仓库地址
##提交代码到远程仓库去
git push -u origin master
一搬情况下提交到这一步第一次提交都会出现报错
! [rejected] master -> dev-gaochao (fetch first)
error: failed to push some refs to '...........'
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.
一脸蒙蔽,本来开开心心以为终于提交成功,原来成功的大门口还有一个坑 不得不跳的坑!废话不多说,出现这个报错大概原因就是因为你在创建远程仓库的时候创建了README.md文件,然后你上传的本地文件夹没有这个文件,所以出现这个报错信息
这个报错信息我统计了一下一共有两种解决办法
第一:将线上、线下代码进行合并
git pull --rebase origin 仓库名
然后在push
git push origin master 仓库名
然后你就会发现成功了
第二种方法:暴力提交
git push -f origin master
failed to push some refs to git
代码提交到仓库时候出现这个报错是因为你创建远程仓库的时候有创建一个readme文件,而本地没有,所以需要先把远程仓库的这个文件给弄到本地来,直接执行命令:git pull --rebase origin master 执行之后再重新 git push -u origin master这一步的操作也使用于上边的报错,上边的这个报错也是因为本地没有readme文件
这种方法提交一定要注意,如果你是第一次提交可以这样做,因为你的仓库里边本来就是空的 也没事,但是如果你是更新代码上去,建议走第一条路,这样可以保住小命,不至于被其他猴子乱刀砍死 行了就这样吧