git 推送功能分支时未能推送一些引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17906704/
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
Failed to push some refs when pushing feature branch
提问by Hurve
What can I do to avoid getting the following message when I push a feature branch a second time:
当我第二次推送功能分支时,我该怎么做才能避免收到以下消息:
To https://github.com/xxx/git_test.git
! [rejected] feature_branch -> feature_branch (non-fast-forward)
error: failed to push some refs to 'https://github.com/xxx/git_test.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.
What I do is this:
我做的是这样的:
git pull origin sprint_branch1
git checkout -b feature_branch
date > a.txt
git add a.txt
git commit -m 'added date'
git push origin feature_branch
Somebody do a code review for my feature and somebody else do changes to the sprint_branch in the mean time:
有人对我的功能进行了代码,同时其他人对 sprint_branch 进行了更改:
git checkout sprint_branch1
date > a.txt
git add a.txt
git commit -m 'added another date'
git push origin sprint_branch1
I need to improve my feature so I do
我需要改进我的功能所以我这样做
git checkout feature_branch
git fetch origin
git rebase origin/sprint_branch1
I get merge conflicts and do:
我遇到合并冲突并执行以下操作:
nano a.txt # removing inserted merge tags
git add a.txt
git rebase --continue
then I improve my feature
然后我改进我的功能
date >> a.txt
git add a.txt
git commit -m 'add another date again'
I like to push my feature_branch for a second review
我喜欢推送我的 feature_branch 进行第二次审核
git push origin feature_branch
However I get the error message mentioned at the top. Git recommend me to use git pull, but other people recommends me to use the rebase workflow. So what should I do to push the feature_branch? Should I create a new branch named feature_branch_v2 and push that? Do I manually need to remember what files to git add in that case or should I add everything (creating a messy commit)? Is there a better way to push without getting this error message?
但是我收到了顶部提到的错误消息。Git 推荐我使用 git pull,但其他人推荐我使用 rebase 工作流。那么我应该怎么做才能推送 feature_branch 呢?我应该创建一个名为 feature_branch_v2 的新分支并推送它吗?在这种情况下,我是否需要手动记住要 git 添加的文件,还是应该添加所有内容(创建混乱的提交)?有没有更好的推送方式而不会收到此错误消息?
采纳答案by Praveenram Balachandar
Git says
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.
You might just need to use the rebase workflow to pull rebase from the remote branch after you have made the commit, then push to remote.
您可能只需要在提交后使用 rebase 工作流从远程分支拉取 rebase,然后推送到远程。
git commit -m 'add another date again'
git pull --rebase
This may lead to rebase conflicts which you would have to solve and then continue rebase. This is primarily cause the version of tree in the sprint_branch1 is behind the feature_branch.
这可能会导致变基冲突,您必须解决这些冲突,然后继续变基。这主要是因为 sprint_branch1 中的树版本落后于 feature_branch。
You should make sure the right commits are going in as well. In a general sense when you are merging with the sprint_branch1, its better to do a
您还应该确保正确的提交也在进行中。一般来说,当你与 sprint_branch1 合并时,最好做一个
git checkout feature_branch
git merge sprint_branch1
This instead of rebase, since rebase rewrites your commits which could cause issues. After you merge if you just push it should work fine.
这不是 rebase,因为 rebase 会重写您的提交,这可能会导致问题。合并后,如果你只是推送它应该可以正常工作。
EDIT 1:
编辑 1:
If you need to rebase and still avoid this error, you can use
如果您需要变基并仍然避免此错误,则可以使用
git push --force origin feature_branch
However this is not recommended especially in distributed teams, as it will rewrite the remote branch with your local branch irrespective of any changes others might have pushed to it.
然而,这在分布式团队中尤其不推荐,因为它会用您的本地分支重写远程分支,而不管其他人可能推送到它的任何更改。
回答by Chronial
This is where you went wrong:
这是你出错的地方:
git rebase origin/sprint_branch1
You should not rebase published branches. This command should have been a
您不应该对已发布的分支进行变基。这个命令应该是一个
git merge origin/sprint_branch1
In general you should be careful with git rebase
– there seems to be some kind of religion around it, even though it is a very dangerous tool.
一般来说,你应该小心git rebase
——它周围似乎有某种宗教信仰,尽管它是一种非常危险的工具。
How can you proceed?
你怎么继续?
If you are absolutely surenobody else is going to touch the feature branch again and nobody did any changes to it since your last pull, you can just do
git push -f
That will overwrite the HEAD on the server with your HEAD.
If you are sure that there were no changes since your last pull, but other people use your branch, you can do the above and tell everybody with a copy of your branch that they need to run
git fetch origin git checkout feature_branch git reset --hard origin/feature_branch
That will erase all their local changes since their last push though.
The safest way would be for you to rename your local
feature_branch
to somethengi else, find the commits you added, branch of the currentorigin/feature_branch
andcherry-pick
all your changes.
如果您绝对确定没有其他人会再次触及该功能分支并且自上次拉取以来没有人对其进行任何更改,那么您可以这样做
git push -f
这将用您的 HEAD 覆盖服务器上的 HEAD。
如果您确定自上次拉取以来没有任何更改,但其他人使用您的分支,您可以执行上述操作并告诉每个拥有您的分支副本的人他们需要运行
git fetch origin git checkout feature_branch git reset --hard origin/feature_branch
这将消除自上次推送以来的所有本地更改。
最安全的方法是将您的本地重命名
feature_branch
为 somethengi else,找到您添加的提交、当前分支origin/feature_branch
和cherry-pick
所有更改。
Run gitk feature_branch origin/feature_branch
to get an understanding of what is going on.
运行gitk feature_branch origin/feature_branch
以了解正在发生的事情。