在推送功能分支时无法推送一些参考 [英] Failed to push some refs when pushing feature branch

查看:241
本文介绍了在推送功能分支时无法推送一些参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以做些什么来避免在第二次推送功能分支时收到以下消息:

 到https: //github.com/xxx/git_test.git 
! [rejected] feature_branch - > feature_branch(非快进)
错误:无法将一些文件推送到'https://github.com/xxx/git_test.git'
提示:由于当前提示分支在
提示后面:它的远程对手。合并远程更改(例如'git pull')
提示:再次按下之前。
提示:有关详细信息,请参阅'git push --help'中的'关于快速转发的注意事项'。

我所做的是:

  git pull origin sprint_branch1 
git checkout -b feature_branch

date> a.txt
git add a.txt
git commit -m'添加日期'

git push origin feature_branch

有人对我的功能进行了代码审查,其他人在同时对sprint_branch进行了更改:

  git checkout sprint_branch1 
日期> a.txt
git add a.txt
git commit -m'添加了另一个日期'
git push origin sprint_branch1

我需要改进我的功能,所以我做了

  git checkout feature_branch 
git fetch origin
git rebase origin / sprint_branch1

我得到合并冲突并执行:

  nano a.txt#删除插入的合并标记
git添加a.txt
git rebase - 继续

然后我改进我的功能

 日期>> a.txt 
git add a.txt
git commit -m'再次添加另一个日期'

我喜欢推动我的feature_branch进行第二次评论

  git push origin feature_branch 

但是,我收到了顶部提到的错误消息。 Git推荐我使用git pull,但其他人建议我使用rebase工作流程。那么我应该怎么做推动feature_branch?我应该创建一个名为feature_branch_v2的新分支并推送它吗?在这种情况下,我是否需要手动记住git添加的文件,还是应该添加所有内容(创建一个混乱的提交)?有没有更好的方法推送,而不会收到此错误消息?

解决方案

这是您出错的地方:

  git rebase origin / sprint_branch1 



你不应该重新发布已发布的分支。这个命令应该是一个

  git merge origin / sprint_branch1 

一般而言,您应该小心 git rebase - 它周围似乎有某种宗教信仰,尽管这是一个非常危险的工具。



你如何继续?


  • 如果您绝对确信没有其他人再次触摸功能分支,自上次拉出后没有人对其进行任何更改,您可以执行

      git push -f 

    这会覆盖

  • 如果您确定上次拉取后没有变化,但其他人使用您的分支,您可以执行上面的操作,并告诉每个人你需要运行的分支副本

      git fetch origin 
    git checkout feature_branch
    git reset --hard origin / feature_branch

    这将消除自上次推送以来所有的本地更改。

  • 最安全的方法是让您重命名本地 feature_branch 添加到somethengi else,找到您添加的提交,当前 origin / feature_branch 和<$ c $的分支c $> b
    $ b

    运行 gitk feature_branch origin / feature_branch 以了解正在发生的事情。


    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:

    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

    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?

    解决方案

    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.

    How can you proceed?

    • If you are absolutely sure nobody 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 current origin/feature_branch and cherry-pick all your changes.

    Run gitk feature_branch origin/feature_branch to get an understanding of what is going on.

    这篇关于在推送功能分支时无法推送一些参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆