git 无法推送到 GitHub - 一直说需要合并
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10298291/
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
Cannot push to GitHub - keeps saying need merge
提问by user1353717
I'm new to GitHub. Today I met some issue when I was trying to push my code to GitHub.
我是GitHub 的新手。今天我在尝试将代码推送到 GitHub 时遇到了一些问题。
Pushing to [email protected]:519ebayproject/519ebayproject.git
To [email protected]:519ebayproject/519ebayproject.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to '[email protected]:519ebayproject/519ebayproject.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
I have not pushed anything in the repository yet, so why do I need to pull something?
我还没有在存储库中推送任何东西,那么为什么我需要拉一些东西呢?
回答by Nick Rolando
This can cause the remote repository to lose commits; use it with care.
这可能会导致远程存储库丢失提交;小心使用它。
If you do not wish to merge the remote branch into your local branch (see differences with git diff), and want to do a force push, use the push command with -f
如果您不希望将远程分支合并到您的本地分支(请参阅与git diff 的差异),并且想要进行强制推送,请使用带有 -f的push 命令
git push -f origin <branch>
where origin
is the name of your remoterepo.
origin
您的远程仓库的名称在哪里。
Usually, the command refuses to update a remote ref that is not an ancestor of the local ref used to overwrite it. This flag disables the check. This can cause the remote repository to lose commits; use it with care.
通常,该命令拒绝更新不是用于覆盖它的本地 ref 的祖先的远程 ref。此标志禁用检查。这可能会导致远程存储库丢失提交;小心使用它。
回答by Jake Greene
As the message tells you,
正如消息告诉你的那样,
Merge the remote changes (e.g. 'git pull')
合并远程更改(例如“git pull”)
Use git pull
to pull the latest changes from the remote repository to your local repository. In this case, pulling changes will require a merge because you have made changes to your local repository.
用于git pull
将远程存储库中的最新更改拉到本地存储库。在这种情况下,拉取更改将需要合并,因为您对本地存储库进行了更改。
I'll provide an example and a picture to explain. Let's assume your last pull from origin/branch was at Commit B. You have completed and committed some work (Commit C). At the same time, someone else has completed their work and pushed it to origin/branch (Commit D). There will need to be a merge between these two branches.
我将提供一个例子和一张图片来解释。假设您最后一次从原点/分支拉取数据是在提交 B。您已经完成并提交了一些工作(提交 C)。同时,其他人已经完成了他们的工作并将其推送到源/分支(Commit D)。这两个分支之间需要合并。
local branch: --- Commit C / / / origin/branch: Commit A ------ Commit B ---- Commit D
local branch: --- Commit C / / / origin/branch: Commit A ------ Commit B ---- Commit D
Because you are the one that wants to push, Git forces you to perform the merge. To do so, you must first pull the changes from origin/branch.
因为您是想要推送的人,所以 Git 会强制您执行合并。为此,您必须首先从源/分支中提取更改。
local branch: --- Commit C -- Commit E / / / / / / origin/branch: Commit A ------ Commit B ---- Commit D
local branch: --- Commit C -- Commit E / / / / / / origin/branch: Commit A ------ Commit B ---- Commit D
After completing the merge, you will now be allowed to fast-forward origin/branch to Commit E by pushing your changes.
完成合并后,您现在可以通过推送您的更改将源/分支快进到 Commit E。
Git requires that you handle merges yourself, because a merge may lead to conflicts.
Git 要求您自己处理合并,因为合并可能会导致冲突。
回答by AYK
Have you updated your code before pushing?
您在推送之前更新了代码吗?
Use git pull origin master
before you push anything.
git pull origin master
在你推动任何东西之前使用。
I assume that you are using origin
as a name for your remote.
我假设您将其origin
用作遥控器的名称。
You need to pull before push, to make your local repository up-to-date before you push something (just in case someone else has already updated code on github.com
). This helps in resolving conflicts locally.
您需要在推送之前先拉取,以便在推送某些内容之前使本地存储库保持最新状态(以防其他人已经更新了 上的代码github.com
)。这有助于在本地解决冲突。
回答by prayagupd
This normally happens when you git commit
and try to git push
changes before git pulling
on that branch x
where someone else has already made changes.
这通常发生在您之前git commit
尝试在其他人已经进行更改的分支上进行更改时。git push
git pulling
x
The normal flow would be as below,
正常流程如下,
STEP 1: git stash
your local uncommitted changes on that branch.
第 1git stash
步:您在该分支上的本地未提交更改。
STEP 2: git pull origin branch_name -v
to pull and merge
to locally committed changes on that branch (give this merge some message, and fix conflicts if any.)
STEP 2:git pull origin branch_name -v
以pull and merge
(对那支本地提交的变化。给这个合并的一些消息,并修正冲突,如果有的话)
STEP 3: git stash pop
the stash
ed changes (Then you can make commits on popped files if you want or push already committed changes (STEP4) first and make new commit to files later.)
STEP 3:git stash pop
在stash
编的变化(然后,你可以,如果你想或推已经提交的修改(STEP4)首先做出新的承诺文件,后来就弹出的文件提交。)
STEP 4: git push origin branch_name -v
the merged changes.
第 4 步:git push origin branch_name -v
合并的更改。
Replace branch_name
with master
(for master
branch).
替换branch_name
为master
(用于master
分支)。
回答by Smit Patel
First and simple solution (Not Recommended)
第一个简单的解决方案(不推荐)
- Try this command
git push -f origin master
. - This command will forcefully overwrite remote repository (GitHub)
- 试试这个命令
git push -f origin master
。 - 此命令将强制覆盖远程存储库 (GitHub)
Recommended solution
推荐方案
- Run these commands:
- 运行这些命令:
git pull --allow-unrelated-histories //this might give you error but nothing to worry, next cmd will fix it
git add *
git commit -m "commit message"
git push
If this doesn't work then follow along
如果这不起作用,请继续
- Delete
.git
directory from the folder. Then execute these commands:
git init git add . git commit -m "First Commit" git remote add origin [url] git push -u origin master
.git
从文件夹中删除目录。然后执行这些命令:
git init git add . git commit -m "First Commit" git remote add origin [url] git push -u origin master
OR
或者
git push -f origin master
Only use git push -f origin master
if -u
dont work for you.
仅git push -f origin master
在-u
不适合您时使用。
This will solve almost any kind of errors occurring while pushing your files.
这将解决推送文件时发生的几乎所有类型的错误。
回答by Teo
Sometimes we forgot the pulling and did lots of works in the local environment.
有时我们忘记了拉动,而是在本地环境中做了很多工作。
If someone want to push without pull,
如果有人想推而不拉,
git push --force
is working. This is not recommended when working with other people, but when your work is a simple thing or a personal toy project, it will be a quick solution.
正在工作。与其他人一起工作时不建议这样做,但是当您的工作是简单的事情或个人玩具项目时,这将是一个快速的解决方案。
回答by xiatica
Some of you may be getting this error because Git doesn't know which branch you're trying to push.
你们中的一些人可能会收到此错误,因为 Git 不知道您要推送哪个分支。
If your error message also includes
如果您的错误消息还包括
error: failed to push some refs to '[email protected]:jkubicek/my_proj.git'
hint: Updates were rejected because a pushed branch tip is behind its remote
hint: counterpart. If you did not intend to push that branch, you may want to
hint: specify branches to push or set the 'push.default' configuration
hint: variable to 'current' or 'upstream' to push only the current branch.
then you may want to follow the handy tips from Jim Kubicek, Configure Git to Only Push Current Branch, to set the default branch to current.
那么您可能需要遵循 Jim Kubicek 的实用提示,将Git 配置为仅推送当前分支,将默认分支设置为当前分支。
git config --global push.default current
回答by Pyuri Sahu
git pull origin branch_name --rebase
This worked for me -- the command git pull origin branch_name --rebase
will pull changes from remote branch_name at first, then rebase
current branch on the top of it.
这对我git pull origin branch_name --rebase
有用——该命令首先会从远程 branch_name 中提取更改,然后rebase
是它顶部的当前分支。
回答by Bhavuk Mathur
In addition to the answers above, the following worked for me : -
除了上面的答案外,以下内容对我有用:-
Scenario -
设想 -
- I pushed my_branchto origin successfully.
- I made few more changes.
- When I tried to push again, (after doing add, commit of course), I got the above mentioned error.
- 我成功地将my_branch推送到了 origin。
- 我又做了一些改动。
- 当我尝试再次推送时(在添加之后,当然是提交),我收到了上述错误。
Solution -
解决方案 -
1. git checkout **my_branch**
2. git add, commit your changes.
3. git pull origin **my_branch** (not origin, master, or develop)
4. git push origin **my_branch**
回答by Kailash Bhalaki
I had the same problem , what I did was I first pushed it by force by using this
我遇到了同样的问题,我所做的是首先使用它强行推动它
git push --force
I did this after I commited the files and was getting an error as you got.It did commit all the files and it pushed them. Then the next time I was pushing to the github .I did what it asked me to and it was alright then. Hope this works for you too :)
我在提交文件后执行此操作,并且在您收到时收到错误。它确实提交了所有文件并推送了它们。然后下一次我推送到 github 时。我做了它让我做的事情,然后就没事了。希望这对你也有用:)