git 在 Github 中推送新代码的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20939648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Issue pushing new code in Github
提问by Aniruddha
I created a new repository on Github which has only Readme.md file now.
我在 Github 上创建了一个新的存储库,现在只有 Readme.md 文件。
I have a newly created RoR project which I wanted to push to this repository. Following are the commands I gave in my Terminal to execute this along with the error I am getting.
我有一个新创建的 RoR 项目,我想将其推送到此存储库。以下是我在终端中给出的命令以及我得到的错误。
git remote add origin https://github.com/aniruddhabarapatre/learn-rails.git
After which I entered my username and password
之后我输入了我的用户名和密码
git push -u origin master
Error ---
错误 - -
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 (e.g.,
hint: 'git pull') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
This is my first time pushing my code to a Github repository and I'm lost with the errors. I searched few other questions that are asked here, but none of them had issues first time.
这是我第一次将我的代码推送到 Github 存储库,但我对错误感到迷茫。我搜索了这里提出的其他几个问题,但没有一个是第一次出现问题。
回答by Van Mart
If this is your first push
如果这是你的第一次推送
just change the
只是改变
git push **-u** origin master
change it like this!
改成这样!
git push -f origin master
回答by Johnsyweb
When you created your repository on GitHub, you created a README.md, which is a new commit.
当您在 GitHub 上创建存储库时,您创建了一个README.md,这是一个新的提交。
Your local repository doesn't know about this commit yet. Hence:
您的本地存储库还不知道此提交。因此:
Updates were rejected because the remote contains work that you do not have locally.
更新被拒绝,因为远程包含您本地没有的工作。
You may want to find to follow this advice:
您可能希望遵循以下建议:
You may want to first merge the remote changes (e.g., '
git pull
') before pushing again.
您可能希望
git pull
在再次推送之前先合并远程更改(例如,' ')。
That is:
那是:
git pull
# Fix any merge conflicts, if you have a `README.md` locally
git push -u origin master
回答by Ahmad Awais
?? EASY: All you need is a forced push. Because you might have created
readme.md
file on Github and you haven't pulled it yet.
?? 简单:您所需要的只是强制推动。因为你可能已经
readme.md
在 Github 上创建了文件,但你还没有拉取它。
git push -f origin master
And here's a GIF.
这是一个 GIF。
?? BEWARE: Using force
can change the history for other folks on the same project. Basically, if you don't care about a file being deleted for everyone, just go ahead. Especially if you're the only dev on the project.
?? 注意:使用force
可以更改同一项目中其他人的历史记录。基本上,如果您不关心为每个人删除文件,请继续。特别是如果您是该项目的唯一开发人员。
回答by Samarth Shah
Issue a forced push with the command:
使用以下命令发出强制推送:
git push -f origin master
回答by Quercus
Assuming that you added the Readme.md file through the interface provided by github, the readme is not yet in your local folder. Hence, when you try to push to the remote repo, you get an error, because your local repo is lacking the readme file - it's "behind the times", so to speak. Hence, as is suggested in the error message, try "git pull" first. This will pull the readme from the remote repository and merge it with your local directory. After that, you should have no problem pushing to the remote repo (the commands you posted look valid to me).
假设你通过github提供的接口添加了Readme.md文件,你本地文件夹中还没有readme。因此,当您尝试推送到远程存储库时,会出现错误,因为您的本地存储库缺少自述文件 - 可以这么说,它“落后于时代”。因此,正如错误消息中所建议的,首先尝试“git pull”。这将从远程存储库中提取自述文件并将其与您的本地目录合并。之后,您应该可以毫无问题地推送到远程存储库(您发布的命令对我来说是有效的)。
回答by Chathuranga
This is happen when you try to push initially.Because in your GitHub repo have readMe.md or any other new thing which is not in your local repo. First you have to merge unrelated history of your github repo.To do that
当您最初尝试推送时会发生这种情况。因为在您的 GitHub 存储库中有 readMe.md 或任何其他不在本地存储库中的新内容。首先,您必须合并 github 存储库的不相关历史记录。要做到这一点
git pull origin master --allow-unrelated-histories
then you can get the other files from repo(readMe.md or any)using this
然后你可以使用这个从 repo(readMe.md 或任何)获取其他文件
git pull origin master
After that
在那之后
git push -u origin master
Now you successfully push your all the changes into Github repo.I'm not expert in git but every time these step work for me.
现在您成功地将所有更改推送到 Github 存储库中。我不是 git 专家,但每次这些步骤都对我有用。
回答by Rob Mcelvenny
Considering if you haven't committed your changes in a while, maybe doing this will work for you.
考虑到您是否有一段时间没有提交更改,也许这样做对您有用。
git add files
git commit -m "Your Commit"
git push -u origin master
That worked for me, hopefully it does for you too.
这对我有用,希望对你也有用。
回答by zheng fuchao
if you use the git for mac in GUI you can chose Respository->Pull or the "comm+shift+p" to "git pull" first, then publish the source.
如果您在 GUI 中使用 git for mac,您可以先选择 Respository->Pull 或“comm+shift+p”到“git pull”,然后发布源。
回答by Ravi
This error occurs when you push the data from your local directory to your remote git repository by following git command: git push -u origin master
当您通过以下 git 命令将数据从本地目录推送到远程 git 存储库时,会发生此错误: git push -u origin master
As local directory and git remote directory's files conflicted.
由于本地目录和 git 远程目录的文件发生冲突。
Solution :
解决方案 :
After committing all files to staging follow below steps.
将所有文件提交到暂存后,请按照以下步骤操作。
Fetch the files from the remote repository as its conflict with the local working directory.
git pull <remoter-url> <branch-name>
Commit the changes again.
git add -A
git commit -m ‘<comment>'
After committed merge files with both directory you can use
git push -u origin master
从远程存储库中获取文件,因为它与本地工作目录冲突。
git pull <remoter-url> <branch-name>
再次提交更改。
git add -A
git commit -m ‘<comment>'
提交与两个目录的合并文件后,您可以使用
git push -u origin master
This will fix the issue. Thanks.
这将解决问题。谢谢。
回答by Marco Caggiano
I had a similar problem... I resolved it like this (i'm not an git expert so i don't know if it is a right solution, but it worked for me):
我有一个类似的问题......我是这样解决的(我不是 git 专家所以我不知道这是否是一个正确的解决方案,但它对我有用):
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