目录
Flask入门教程HelloFlask地址
没有直接在本地建好仓库再push到GitHub而是两边都建立了仓库然后链接了一下
github建立仓库直接在GitHub的UI界面自己建立就行
本地建立仓库
#在项目文件夹根目录下执行
git init
#创建.gitignore文件
nano .gitignore
#在nano编辑界面写入
#写入内容如下:
*.pyc
*~
__pycache__
.DS_Store
#Control+O和Enter保存,Control+X退出
通过SSH密钥建立本地仓库与GitHub账户的链接
#检查是否已经创建密钥:
cat ~/.ssh/id_rsa.pub
#若显示"No such firle or directory",则执行 ssh-keygen 生成密钥,一路enter
#然后再次执行cat查看指令
$ cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3Nza *******很多很多字符= Alan@Alan(最后显示了一下名字)
#然后复制其文件内容到githubde SSH设置界面
# 从github新建的仓库的code下拉框里复制其SShURL "git@github.com:*******"
git remote add origin git@github.com:******* #星号为GitHub用户名和仓库名
提交commit
#将本地git仓库的默认分支也改成main
#因为GitHub现在新建的仓库直接默认是main而不是master
git config --global init.defaultBranch main
git add .
git commit -m "一个message"
git push origin main #推到远程GitHub仓库
然后问题就出现了,问题就在于。。。我在GitHub的网页界面给远程仓库新建了readme.md 然后发现再次从本地提交commit的时候,push会出错
出错情况:
To github.com:*********git
! [rejected] main -> main (fetch first)
error: failed to push some refs to 'github.com:********.git'
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.
解决办法
Stack Overflow中有一个答案可以解决问题,即由于出错原因在于远程仓库有新建文件,但是本地没有,存在冲突
具体我自己的操作如下,解决问题
#把远程仓库新建的文件拉下来合并
$ git pull origin main
From github.com:*************
* branch main -> FETCH_HEAD
Merge made by the 'ort' strategy.
README.md | 2 ++
1 file changed, 2 insertions(+)
create mode 100644 README.md
(env)
#把本地Git推到远程,完成提交
$ git push origin main
To github.com:**********
59fa362..82f8a3a main -> main
(env)
#我在Python的虚拟环境中所以有一个命令行的(env)的后缀