将提交移到其他分支 [英] Move commit to different branch

查看:66
本文介绍了将提交移到其他分支的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种简单的方法可以将提交从分支A移动到分支B.

I'm wondering if there is a simple way to move commit from branch A to branch B.

我当时在开发"分支上工作,然后创建了一个新分支,比方说"featureA" 开始开发新功能.我进行了2次提交,但是第一个提交应该是去"develop"而不是"featureA",因为这只是一个小问题,但是我忘了更改分支,所以我可以简单地将commit 1移到分支"develop"并删除它来自"featureA"?

I was working on "develop" branch, then created a new one, let's say "featureA" to start developing new feature. I made 2 commits, but first one should have gone to "develop" instead of "featureA" because it was just a small fix, but I forgot to change branch, so can I simply move commit 1 to branch "develop" and remove it from "featureA"?

更改仍未推送,仅在本地进行.

Changes are still not pushed, just local.

我将不胜感激.

推荐答案

在这种特殊情况下,这很容易.要了解原因,请绘制(部分)提交图.

In this particular case it is easy. To see why, draw (part of) the commit graph.

您从这样的事情开始:

...--o--*   <-- develop (HEAD), origin/develop

然后,您创建了一个新的分支名称feature,它指向相同的develop提交*.您将HEAD附加到新分支,得到:

You then created a new branch name, feature, pointing to the same tip-of-develop commit *. You attached your HEAD to the new branch, giving:

...--o--*   <-- develop, feature (HEAD), origin/develop

现在,您进行了两次新提交.让我们称它们为AB,而不是使用它们的真实哈希ID,这些ID大而丑陋且容易忘记.提交A具有提交*作为其父级,提交B具有提交A作为其父级,并且名称feature现在指向提交*:

Now you made two new commits. Let's call them A and B rather than using their real hash IDs, which are big and ugly and forgettable. Commit A has, as its parent, commit *, and commit B has commit A as its parent, and the name feature now points to commit *:

...--o--*   <-- develop, origin/develop
         \
          A--B   <-- feature (HEAD)

不理会feature,让我们告诉Git移动名称develop指向提交A,这是您想对develop进行的次要修复:

Leaving feature alone, let's tell Git to move the name develop to point to commit A, the minor fix you want to have on your develop:

...--o--*   <-- origin/develop
         \
          A   <-- develop
           \
            B   <-- feature

我暂时删除了随附的HEAD,因为有几种方法可以像这样调整develop,并且HEAD可能会在执行操作时重新连接.这是您想要的最后一张图片:完全没有提交更改,但是您的本地名称develop现在指向第一个提交,而feature指向第二个提交.

I've removed the attached HEAD temporarily since there are several ways to adjust develop like this and HEAD may re-attach as we do them. This is the final picture you want: no commits have changed at all, but your local name develop now points to the first commit, while feature points to the second one.

那么:我们应该怎么做?

So: how shall we do it?

一个相当安全的方法(不会丢失任何工作)是首先git checkout develop,然后使用git merge --ff-onlydevelop快进到正确的提交.这是提交A,它比feature后退了一步,因此我们可以执行以下操作:

One quite-safe method (can't lose any work) is to git checkout develop first, then use git merge --ff-only to fast-forward develop to the correct commit. That's commit A, which is one step back from feature, so we can do this:

git checkout develop
git merge --ff-only feature~1

这将使您剩下develop指向提交A(并且您的HEAD附加到develop,因此您必须git checkout feature才能继续).

This will leave you with develop pointing to commit A (and your HEAD attached to develop, so you have to git checkout feature to keep going).

另一种方法是检出develop并使用git reset --hard将标签develop移动到任意提交(同时还更新索引/临时区域和工作树).这基本上与上面的相同,但是即使您的选择不明智,也可以将其移动到任何地方,所以上面的方法可能更可取.

Another way is to check out develop and use git reset --hard to move the label develop to any arbitrary commit (while also updating your index / staging-area and your work-tree). This is essentially the same as above, but it lets you move the name anywhere, even if that's not a sensible move, so the above is probably preferable.

另一种方法是使用git branch -f强制将名称移动到您想要的位置:

Yet another way is to use git branch -f to force move the name to the location you want:

git branch -f develop feature~1

这会将您的HEAD附加到feature,因此您不必git checkout feature即可继续工作.但是,就像git reset方法一样,它也不是很安全,因为它可以让您将名称移动到 any 提交,即使这没有任何意义.

This leaves your HEAD attached to feature, so that you don't have to git checkout feature to keep working. However, like the git reset method, it's not very safe as it lets you move the name to any commit, even if that makes no sense.

最后,有一种偷偷摸摸的方法可以快速签发develop(la git merge --ff-only)而无需签出develop-实际上,您需要留在某些地方其他分支机构可以做到:

Finally, there's a very sneaky way to update develop in a fast-forward fashion (a la git merge --ff-only) without checking out develop—in fact you need to stay on some other branch to do it:

git push . feature~1:develop

这使您的Git调用您自己的Git,并建议它应将名称develop移至指向其散列ID是通过解析feature~1获得的提交.您的Git会在且仅当这是快速前进时才服从您的Git,因为此处没有强制标记.

This has your Git call up your own Git, and propose to itself that it should move the name develop to point to the commit whose hash ID was obtained by parsing feature~1. Your Git will obey your Git if and only if this is a fast-forward, since there is no force flag here.

使用您喜欢的任何一种;最后,除了连接HEAD的地方,它们都达到相同的结果.

Use whichever of these you like; they all achieve the same result in the end, except for where your HEAD is attached.

这篇关于将提交移到其他分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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