简体   繁体   English

Git将我的分叉分支在上游主服务器上

[英]Git rebase my forked branch on upstream master

A certain user on Github has a repository with single master branch. Github上的某个用户拥有一个包含单个主分支的存储库。 I have forked that repository into my own account. 我已将该存储库分配到我自己的帐户中。 I have created a new branch on my own forked repository and have commited some changes to it. 我在自己的分叉存储库上创建了一个新分支,并对其进行了一些更改。

The upstream user has commited changes to their master. 上游用户已将更改提交给其主服务器。 I wish to update my master with those changes. 我希望通过这些更改来更新我的主人。 Additionally, if I understand rebase correctly, I want to rebase my branch on that master. 另外,如果我正确理解rebase ,我想在该master上重新rebase我的分支。 I do not want to merge with master because I have not finished developing my branch. 我不想与master合并,因为我还没有完成我的分支。 Note: The files changed by upstream are different from the files I have changed in my branch, so there should not be an conflicts. 注意:上游更改的文件与我在分支中更改的文件不同,因此不应存在冲突。

I have tried the following: 我尝试过以下方法:

I pulled changes from the upstream user into my local repo: 上游用户的更改提取到我的本地仓库中:
git pull upstream master

I pushed changes from my local master into my forked master: 更改从我的本地主人送到我的分叉主人:
git push origin master

I rebased my local branch on my local master: 我在我当地的主人那里重新定位了当地的分支:
git checkout mybranch
git rebase master

I attempted to push my rebased local branch to my forked branch: 我试图我重新定位的本地分支送到我的分支分支:
git push origin mybranch

However, I get the following error: 但是,我收到以下错误:

To https://github.com/myusername/myforkedrepository.git
 ! [rejected]        mybranch -> mybranch (non-fast-forward)
error: failed to push some refs to 'https://github.com/myusername/myforkedrepository.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.

Note that origin is my forked repository and upstream is the original repositrory. 需要注意的是origin就是我分出的代码库和upstream是原有repositrory。

There is similar question on stackoverflow that suggested a slightly simpler method: stackoverflow上有类似的问题,它提出了一个稍微简单的方法:

git fetch upstream
git rebase upstream/master

This did not give any errors, but it has also not pushed anything to my remote fork. 这没有给出任何错误,但它也没有向我的远程分支推送任何东西。 After trying the above, I again tried 在尝试了上述之后,我再次尝试了

git push origin mybranch

but the same error persisted. 但同样的错误仍然存​​在。

How would I go about doing what I want? 我该如何做我想做的事?

This is how I understand your scenario: 这就是我理解你的场景的方式:

[upstream repo] ==> [forked repo] <==> [local repo]
   master               master            master
                                          mybranch

The arrows indicate the directional flow of commits. 箭头表示提交的方向流。

And given the above you've asked how do you implement your desired dev flow of keeping your changes always on top. 鉴于上述情况,您已经问过如何实现所需的开发流程,始终保持您的更改。

Overall it is inevitable for the master on both the forked and local repos to deviate from the upstream master , if they are is to incorporate the changes on the mybranch topic branch. 总的来说,分叉和本地存储库上的服务器不可避免地偏离上游主服务器 ,如果他们要将更改合并到mybranch主题分支上。

However, when the changes are completely disjoint, as you outline in the question, you can indeed keep your set of changes on top by rebasing and fast-forwarding, when you merge or pull. 但是,当更改完全不相交时,正如您在问题中所概述的那样,当您合并或拉动时,您确实可以通过重新定位和快速转发来保持您的更改集。

0] local master   $ branch mybranch off master 
    1] fork  master   $ git pull --rebase
    2] local master   $ git pull --rebase
    3] local mybranch $ commit commit commit
    4] local mybranch $ git rebase master

Repeat 1-4 or just 3-4 as many times as necessary in your dev flow. 在开发流程中根据需要重复1-4或3-4次。 When you are ready to publish your work to the forked repo master , the upstream master and the forked master will go out of sync, but as long as you've followed 1-4 you should be able to forever keep your changes on top. 当您准备将您的工作发布到分叉的repo主服务器时上游主服务器分叉主服务器将不同步,但只要您遵循1-4,您就应该能够永远保持您的更改。

5] local master $ git merge --ff-only mybranch
    6] local master $ git push origin

And then repeat 1-4 again as many times as necessary. 然后根据需要再次重复1-4次。

A word of caution. 提醒一句。 In order for this to work you may need to reset your branches (or maybe re-clone your repos) to a "clean" state, where you haven't merged and/or pushed yet any of the changes on mybranch to a local or forked master . 为了实现这一点,您可能需要将分支机构(或者可能重新克隆您的存储库)重置为“干净”状态,您尚未将mybranch上的任何更改合并和/或推送到本地或分叉大师

This is because you already published your mybranch branch before this. 这是因为您之前已经发布了mybranch分支。

The solution is simple: Just don't. 解决方案很简单:只是不要。 You want to changed published history, which is never a good idea! 您想要更改已发布的历史记录,这绝不是一个好主意! This could lead to really ugly bugs for other developers who already based work on your public mybranch branch. 对于那些已经在你的公共mybranch分支上工作的其他开发人员来说,这可能会导致非常丑陋的错误。


A technical solution would be: git push --force origin mybranch or deleting the remote branch and pushing it again with git push origin :mybranch and git push origin mybranch 一个技术解决方案是: git push --force origin mybranch或删除远程分支并使用git push origin :mybranch再次推送它git push origin :mybranchgit push origin mybranch

But you don't should do this if mybranch is already published. 但如果mybranch已经发布,你不应该这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM