简体   繁体   English

我分支了错误的分支,如何将更改移动到正确的分支?

[英]I branched off the wrong branch, how do I move my changes to correct branch?

We have a develop branch and a release branch.我们有一个develop分支和一个release分支。

I was asked to make a change, but was later told that the change had to be a hotfix and so I should have branched off the release branch.我被要求进行更改,但后来被告知更改必须是修补程序,因此我应该分支出release分支。

The current situation is this (I drew the diagram myself):目前的情况是这样的(我自己画的图):

git 分支

I really don't want to loose all my work that is currently sitting on my feature-branch .真的不想放弃目前在我的feature-branch上的所有工作。 How do I move all my changes in feature-branch so that they sit in a new branch but one that is branched off release branch?如何移动我在feature-branch中的所有更改,以便它们位于一个新分支中,但另一个分支是从release分支分支出来的?

What have I tried?我尝试了什么?

I tried using rebase --onto but I got the following error:我尝试使用rebase --onto但出现以下错误:

$ git rebase --onto release-branch feature/1 feature/1a $ git rebase --onto 发布分支功能/1 功能/1a

fatal: fatal: no such branch/commit 'feature/1a'致命:致命:没有这样的分支/提交'feature/1a'

To duplicate the commits from feature , and only them (no commit from develop before or after feature forked from it), you can use the following commands:要复制来自feature的提交,并且只复制它们(在从它派生的feature之前或之后没有来自develop的提交),您可以使用以下命令:

git checkout -B feature release      # Checkout `release` and bring `feature` along
git cherry-pick develop..feature@{1} # Retrieve commits from `feature` which are not on `develop`

Try this commands (pseudo-code: apply your naming correctly)试试这个命令(伪代码:正确应用你的命名)

git checkout release
git rebase feature

After these commands, you will have work from feature branch in release branch.在这些命令之后,您将在release分支的feature分支中进行工作。

However if develop has accepted commits after feature branch was checked out, those commits will not be present nor in feature nor in release .但是,如果在检出feature分支后develop已接受提交,则这些提交将不存在,也不存在于featurerelease中。

You will need to rebase your release branch agains master in order to have them in release branch.您需要将release分支重新设置为 master 以便将它们放在release分支中。 By running:通过运行:

git checkout release
git rebase develop

PS Here is the link that describes the rebase command https://git-scm.com/docs/git-rebase PS这里是描述rebase命令https的链接://git-scm.com/docs/git-rebase

I think the best way to do this is to rebase your Feature branch onto Release.我认为最好的方法是将您的功能分支重新定位到发布。 The git rebase command is quite flexible. git rebase 命令非常灵活。 You can tell it where to move your branch, and how much of your branch to move.您可以告诉它要将分支移动到哪里,以及要移动多少分支。

This command will move the Feature branch to branch off Release, starting from the commit after the Develop branch:此命令会将 Feature 分支移至 Release 分支,从 Develop 分支之后的提交开始:

git rebase --onto Release Develop Feature

or alternatively:或者:

git checkout Feature
git rebase --onto Release Develop

No need to create a new branch or delete your original one.无需创建新分支或删除原始分支。

暂无
暂无

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

相关问题 <git>我已经分支了一个分支并想要推送我的更改。 我是将更改推送到原始分支还是合并两个分支?</git> - <GIT> I have branched off a branch and want to push my changes. Do I push changes to the original branch or merge the two branches? 如何打印分支从主分支分支出来的子树? - How do I print the subtree where branch branched off master? 我没有从分支机构分支,而是从分支机构分支 - I didnt branch off of master, instead branched off of a branch Git:我分支出的Change分支 - Git: Change branch I have branched off of 我如何成功地将一个分支变基到一个 (github) 压缩的提交上,其中包含它被分支出来的提交? - How do I successfully rebase a branch onto a (github) squashed commit containing the commits it was branched off of? 如何更改我分支的git分支? - How do I change the git branch from which I branched? 如何将我的提交从“无分支”“移动”到实际分支? - How do I "move" my commits from "no branch" to an actual branch? 意外地分支了错误的分支,当我想合并到主服务器时,我必须合并两个分支 - Accidentally branched off of the wrong branch, and when I want to merge into the master I have to merge both branches 我在错误的分支上进行了更改。 我还没有承诺。 如何将我的更改提交到预期的分支? - I made changes while on the wrong branch. I haven't committed yet. How do commit my changes to the intended branch? 我在git的错误分支上创建了一个分支,并将旧分支合并到master中,如何从另一个分支中删除提交? - I created a branch off of the wrong branch in git and already merged the old branch into master, how do I remove the commits from the other branch?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM