创建私有仓库可以参考这篇文章的前面7步

关于Github私有仓库的创建

然后我想在私有仓库上传文件该怎么办呢?

在你的文件夹右键git bash

  1. 初始化git
git init
  1. 添加本地文件
git add .
  1. 上传本地
git commit -m "first commit"
  1. 设置branch(我仓库的branch是main,也有master的,根据实际情况修改就好了)
git branch -m main
  1. 远程连接
git remote add origin XXX

这里的XXX可以用https地址也可以用ssh地址,我用的是ssh地址,这里需要配置一下ssh keys,可以看这篇文章:GitHub 添加 SSH keys

用git给github私有仓库上传文件-LMLPHP

  1. 推送到远程仓库
git push -u origin main

这一步我报错了

用git给github私有仓库上传文件-LMLPHP

! [rejected] main -> main (non-fast-forward)

解决办法可以看github官方文档的解释:
github中文版文档:处理非快进错误
github英文版文档:Dealing with non-fast-forward errors

我使用的命令:

git fetch origin main
git merge origin FETCH_HEAD
git pull --rebase origin main

(还不太懂原理,后面继续学习一下)

  1. 继续推送
git push

又出错了: fatal: The current branch main has no upstream branch. To push the current branch and set the remote as upstream,

用git给github私有仓库上传文件-LMLPHP
查看博客的方法

git push报错:The current branch master has no upstream branch

我也看看我有什么分支:

本地分支:

git branch

用git给github私有仓库上传文件-LMLPHP

远程分支:

git branch -a

用git给github私有仓库上传文件-LMLPHP
所以到底推送哪个呢?

解决方法(我使用的)

git push --set-upstream origin main

完美解决!!!
用git给github私有仓库上传文件-LMLPHP


其他参考

09-24 13:02