前言
将本地项目推送到GitHub上。
一、步骤
1.1 在GitHub新建一个仓库
- 新建仓库
- 复制仓库地址,下面要用到
1.2 本地仓库初始化
打开git bash,cd切换到需要推送到GitHub的项目的目录下,然后依次执行下面的命令 :
-
touch README.md
如果GitHub仓库没创建README.md,可以在本地手动创建。
-
git init
初始化本地仓库
-
git add .
最后一个点代表当前目录下的全部文件。作用是把全部文件添加到暂存区。
-
git commit -m “提交说明”
把暂存区的文件提交到本地仓库。
1.3 连接远程仓库并推送
同样依次执行下面的命令:
-
git remote add origin 远程仓库地址
origin是给远程仓库取的一个别名,远程仓库地址就是上面在GitHub复制的仓库地址。该命令作用是让本地仓库和远程仓库相连接。
-
git push -u origin master
把本地库的内容推送到远程,用git push命令,实际上是把当前分支master推送到远程。由于远程库是空的,我们第一次推送master分支时,加上了-u参数,Git不但会把本地的master分支内容推送的远程新的master分支,还会把本地的master分支和远程的master分支关联起来,在以后的推送或者拉取时就可以简化命令(直接用git pull或者git push)。
二、出现的问题
2.1 问题一
执行 git push -u origin master 命令推送:
$ git push -u origin master
Fatal: HttpRequestException encountered.
Username for 'https://github.com': Samven7
To https://github.com/Samven7/mutichat-system.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/Samven7/mutichat-system.git'
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.
提示push失败,在push执行前要先执行pull拉取远程代码。所以执行 git pull origin master 命令:
$ git pull origin master
From https://github.com/Samven7/mutichat-system
* branch master -> FETCH_HEAD
fatal: refusing to merge unrelated histories
意思是拒绝合并没有联系的历史,表明本地和远程现在是两个不同的项目。
解决方案(两种选其一即可):
-
针对 refusing to merge unrelated histories 问题
原因:远程仓库和本地仓库没有联系,表明本地和远程现在是两个不同的项目。
允许合并不相关的内容:
$ git pull --allow-unrelated-histories Auto-merging README.md Vim: Error reading input, exiting... Vim: preserving files... Vim: Finished. error: There was a problem with the editor 'vi'. Not committing merge; use 'git commit' to complete the merge.
因为pull拉取到新内容,这里让我们git commit提交到本地仓库:
$ git commit -m "get origin readme.md" [master 2b85058] get origin readme.md
然后就可以成功推送了:
$ git push -u origin master Fatal: HttpRequestException encountered. Username for 'https://github.com': Samven7 Counting objects: 58, done. Delta compression using up to 4 threads. Compressing objects: 100% (56/56), done. Writing objects: 100% (58/58), 34.69 KiB | 0 bytes/s, done. Total 58 (delta 15), reused 0 (delta 0) remote: Resolving deltas: 100% (15/15), done. To https://github.com/Samven7/mutichat-system.git 10de11a..2b85058 master -> master Branch master set up to track remote branch master from origin.
-
针对 non-fast-forward 问题
原因:在GitHub初始化仓库时,创建了README.md文件,本地不能直接推送。
强行推送,本地仓库代码强行覆盖远程仓库:
$ git push -f origin master
2.2 问题二
每次推送都要输入GitHub的用户名和密码:
$ git push -u origin master
fatal:HttpRequestException encountered.
Username for 'https://github.com':
原因:Github禁用了TLS v1.0 and v1.1,必须更新Windows的git凭证管理器才行。
解决方案:
点击进入下面的地址:
https://github.com/Microsoft/Git-Credential-Manager-for-Windows/releases/tag/v1.14.0
点击下载安装GCMW-1.14.0.exe即可:
参考
https://blog.csdn.net/north1989/article/details/53471439
https://segmentfault.com/a/1190000015795168
https://blog.csdn.net/weixin_39800144/article/details/80904068