在Github推送新代码的问题

我在Github上创build了一个只有Readme.md文件的新库。

我有一个新创build的RoR项目,我想推到这个仓库。 以下是我在terminal中执行的命令,以及我所得到的错误。

git remote add origin https://github.com/aniruddhabarapatre/learn-rails.git 

之后我input了我的用户名和密码

 git push -u origin master 

错误—

 To https://github.com/aniruddhabarapatre/learn-rails.git ! [rejected] master -> master (fetch first) error: failed to push some refs to 'https://github.com/aniruddhabarapatre/learn-rails.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 merge the remote changes (eg, hint: 'git pull') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details. 

这是我第一次把我的代码推到一个Github仓库,我迷失了错误。 我search了几个在这里被问到的其他问题,但没有一个第一次有问题。

当你在GitHub上创build你的仓库时 ,你创build了一个README.md ,这是一个新的提交 。

你的本地仓库还不知道这个提交。 因此:

更新被拒绝,因为远程包含本地没有的工作。

你可能想要find遵循这个build议:

在再次推送之前,您可能需要首先合并远程更改(例如“ git pull ”)。

那是:

 git pull # Fix any merge conflicts, if you have a `README.md` locally git push -u origin master 

如果这是你的第一个推动

只是改变

 git push **-u** origin master 

像这样改变它!

 git push -f origin master 

使用以下命令发出强制推送:

 git push -f origin master 

E️EASY:你所需要的只是一个强制推动。 因为你可能在Github上创build了readme.md文件,所以你还没有把它拉出来。

 git push -f origin master 

假设您通过github提供的接口添加了Readme.md文件,则自述文件还不在您的本地文件夹中。 因此,当你试图推送到远程仓库时,你会得到一个错误,因为你的本地仓库缺less自述文件 – 这是“时代背后”,可以这么说。 因此,正如在错误信息中所build议的,先尝试“git pull”。 这将从远程存储库中提取自述文件并将其与本地目录合并。 之后,你应该没有问题推到远程回购(你发布的命令看起来对我有效)。

如果在GUI中使用git for mac,则可以先selectRespository-> Pull或“comm + shift + p”为“git pull”,然后发布源代码。

考虑一下你是否在一段时间内没有做出改变,也许这样做会对你有用。

 git add files git commit -m "Your Commit" git push -u origin master 

这对我有效,希望它也适合你。

我挣扎了一个多小时这个错误! 下面是帮我解决的。 所有这些,而我的工作目录是我已经克隆在我的系统上的回购。

如果你正在添加文件到现有的存储库** 1.我把我添加到我的仓库的所有东西都放到了我的GitHub文件夹中:

混帐拉


输出是一些自述文件file1 file2

  1. 我复制(拖放)我的新文件(我想推的文件)到我的克隆库(GitHub回购)。 当你将这个回购你应该看到你的新旧文件。

例如。 一些自述文件file1 file2 newfile1 newfile2

  1. git添加“newfile1”“newfile2”

  2. [可选] git状态,这将确保您要添加的文件是正确的阶段或不输出


在分支主人你的分支是最新的“起源/主”。 要提交的更改:(使用“git reset HEAD …”来取消)

  new file: newfile1 new file: newfile2 

5.git提交-m“你想给的任何描述”6.git push

所有我的新文件以及更旧的文件都在我的回购库中看到。

按照git命令从本地目录中将数据推送到远程git存储库时会发生此错误: git push -u origin master

由于本地目录和git远程目录的文件冲突。

解答:

将所有文件提交到分段后,请按照以下步骤操作。

  1. 从远程存储库中获取文件,作为与本地工作目录的冲突。

    • git pull <remoter-url> <branch-name>
  2. 再次提交更改。

    • git add -A
    • git commit -m '<comment>'
  3. 提交合并文件后,您可以使用这两个目录

    • git push -u origin master

这将解决这个问题。 谢谢。

我有一个类似的问题…我解决了这样的问题(我不是一个混帐专家,所以我不知道这是一个正确的解决scheme,但它为我工作):

 git pull origin master --allow-unrelated-histories git merge origin origin/master git rm README.md git commit -m 'removed readme.md' git push origin master