1

I am new to Git and would like some help. This question is similar to Move the most recent commit(s) to a new branch with Git but that question asks about the most recent commits. This is not what I need. Let me explain further...

I have a branch, lets call it Feature1 branch. Feature1's parent is the master branch and Feature1 has the following commits:

  • A
  • B
  • C
  • D
  • E
  • F

However, the A and B commits don't really belong in that particular branch and should have really been in a different branch that would be the child of the Feature1 branch. Let's call this supposed branch that A and B belong to as Feature2 branch. Again, this branch has to come off of the Feature1 branch and NOT from the master branch.

This Feature2 branch has not been created yet. However, the contents of Feature1 have already been pushed to remote. My question basically is... How do I move A and B commits to a new branch called Feature2?

Note: C, D, E, and F don't rely on A and B. A and B change completely different files than the other commits in the branch.

Yang K
  • 407
  • 4
  • 13

1 Answers1

1

Assuming that A and B are not the most recent commit, you have:

m--m                   (master)
    \
      a--b--c--d--e--f (feature1)

Since feature1 has already been pushed, it would be risky to rebase it to rewrite its history.

You can then revert a--b in order for feature1 to not display those changes:

git checkout feature1
git revert a^..b

m--m                      (master)
    \
      a--b--c--d--e--f--g (feature1)

Then you can create a feature2 branch from feature1, and cherry-pick a--b on it:

git checkout -b feature2
git cherry-pick a^..b

m--m                      (master)
    \
      a--b--c--d--e--f--g (feature1)
                         \
                          a'--b' (feature2)
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250