上传本地文件到github仓库

第一步:新建仓库

给仓库一个名字,备注

得到仓库地址:

https://github.com/Lucasli2018/java-1-mybatis.git

  

第二步:进入要上传的文件夹,初始化上传文件夹仓库

通过git init新建自己的本地仓库

git init

 第三步:添加所有文件到git

通过git add . 把文件添加到暂存区,不要忘记后面的小数点

通过git commit -m "first commit",将文件提交到仓库,引号内为提交说明

git add .
git commit -m "first commit"

  

注意:小数点表示所有文件,也可以指定文件git add 文件夹名

 第四步:连接到guthub仓库

通过 git remote add origin 你的远程地址来关联到远程仓库

git remote add origin https://github.com/Lucasli2018/********.git

  

如果不是第一次上传,可能会提示一下信息:

fatal: 远程 origin 已经存在。

  

这时只需要将远程配置删除,重新添加即可;

git remote rm origin
git remote add origin https://github.com/Lucasli2018/*******.git

  

第五步:输入“git push -u origin master”,上传项目到Github,将本地库的内容推送到远程

这里会要求输入Github的账号密码,按要求输入就可以。

git push -u origin master

  

如果提示错误:

error: 无法推送一些引用到 'https://github.com/Lucasli2018/*********.git'

更新被拒绝,因为远程仓库包含您本地尚不存在的提交。这通常是因为另外一个仓库已向该引用进行了推送。再次推送前,您可能需要先整合远程变更

则可以尝试强行上传:

git push -u origin +master

  

错误提示

$ git push -u origin master
error: src refspec master does not match any.
error: failed to push some refs to 'https://github.com/Lucasli2018/java-1-mybatis.git'

  

原因:

本地仓库为空

解决:先commit本地仓库文件

git commit -m "first commit"

  

到GitHub上的远程仓库进行刷新即可看到我们上传的项目

ok

参考:

https://www.cnblogs.com/roadofstudy/p/7660583.html

https://www.jianshu.com/p/3aca836b932e

原文地址:https://www.cnblogs.com/1906859953Lucas/p/11014832.html