如何在新的Github存储库中推送现有项目?

时间:2015-07-22 01:12:55

标签: git github

我正在研究git,我在使用我在Github中创建的新存储库中推送现有文件时遇到了问题。

这就是我所做的:

  1. 我在项目- git init
  2. 中执行此操作
  3. 我在本地存储库- git add .
  4. 中添加了文件
  5. 我提交- git commit -m "This is the base code for the project"
  6. 然后我在我的Github页面https://github.com/myname/my_projects.git
  7. 中复制github存储库URL
  8. 然后我指出了存储库- git remote add origin https://github.com/myname/my_projects.git
  9. 然后我推送文件- git push origin master
  10. 然后在做推后我有这个错误:

    λ git push origin master
    Username for 'https://github.com': myname
    Password for 'https://myname@github.com':
    To https://github.com/myname/my_projects.git
     ! [rejected]        master -> master (fetch first)
    error: failed to push some refs to 'https://github.com/myname/my_projects.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.
    

    你能帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:3)

git push --force origin master如果您确定要使用本地内容覆盖github遥控器上的内容。

答案 1 :(得分:2)

您可能使用README文件初始化了Github上的repo。如果是这样,您可以按照建议的早期答案进行操作,或者您可以先使用:

NumberDecimalDigits

您的分支机构不是最新的,因为遥控器有自述文件,而您没有此文件本地。拉完后,你必须再次提交,但是这样做会告诉Git你的本地分支是否与远程分支保持同步。

在此之后你可以像往常一样推动:

git pull origin master