首页 > git > 如何将分支HEAD从一个分支移动到另一个分支

如何将分支HEAD从一个分支移动到另一个分支 (How to move branch HEAD from one branch to anonther)

2018-07-22 git

问题

A在Git中有这样的层次结构:

- master \
         - feature1 
         - feature2 \
                    - mybranch

feature2有来自feature1的20个额外提交。

我创建了新的分支(mybranch)并意外地发现它是由错误的功能创建的。我对新文件的所有更改,并不影响feature1或feature2中的任何文件。

如何将我的分支HEAD从feature2移动到feature1?

解决方法

您应该使用git rebase --onto完全相同的功能。

首先,一定要检查'mybranch'。然后:

git rebase feature2 --onto feature1

你告诉git在'feature2'和当前分支'mybranch'的尖端之间进行所有提交,并将它们重新应用到'feature2'。

Ps:这不是你应该做的“正常”变换(交互与否),或者你最终会得到'feature2'引入的所有20次提交

问题

A have such hierarchy in Git:

- master \
         - feature1 
         - feature2 \
                    - mybranch

feature2 have 20 additional commits from feature1.

I created new branch(mybranch) and accidentally found that it was created from wrong feature. All my changes in new files, and doesn't affect any files from feature1 or feature2.

How I can move my branch HEAD from feature2 to feature1?

解决方法

You should use the git rebase --onto feature that is exactly to do that.

First, be sure to have 'mybranch' checked out. Then:

git rebase feature2 --onto feature1

You are telling git to take all the commits between 'feature2' and the tip of the current branch 'mybranch' and re-apply them onto 'feature2'.

Ps: That's not a 'normal' rebase that you should do (interactive or not) or you will end up with also all the 20 commits introduced by 'feature2'

相似信息