0

I have the following situation:

(M1)---(M2)---(M3)---(M4) (master)
  \
   \
   (A)---(B)---(C)---(D) (feature)

I branched off of master and started developing a new feature, which I stopped at B. I then started working on a new feature, of which I committed C and D. I haven't pushed anything since A.

But, oops, I forgot to create a new branch from master for the second feature! I now need to push the second feature, but the first one isn't finished yet. Since it's all on the same branch, I can't push, else the unfinished the first feature will go with it. So what I think I need to get to is something like this:

   (A)---(B) (unfinished feature)
   /
  /
(M1)---(M2)---(M3)---(M4) (master)
  \
   \
   (C)---(D) (finished feature)

From some research, I came across the following: git rebase --onto M1 C. I don't think this is right, since I want everything after C to be a new branch from M1, rather than replaying C and D on master. I want to keep master and the first feature unchanged, just create a new branch for the second feature.

What is the right thing to do here, and is it even possible?

Edit: Whoops, got my structure wrong. I actually did create a branch for the second feature, but I created it off of the first feature rather than master. So it looks like:

(M1)---(M2)---(M3)---(M4) (master)
  \
   \
   (A)---(B)    (feature 1)
     \
      \
      (C)---(D)  (feature 2))

I want feature 2 to be independent of feature 1, so not including commit A.

Kumalh
  • 519
  • 1
  • 3
  • 14

2 Answers2

0

But, oops, I forgot to create a new branch from master for the second feature! I now need to push the second feature

No problems, create branch now and your working directory and staging are will be with the new code.

Commit and save your work. Now you can go back to your original branch and do your work there, When you done and you want to import your changes as well use the git-cherry-pick to import your commits to the new branch.

git cherry-pick <SHA-1>

Apply the change introduced by the commit(s) at the tip of the master branch and create a new commit(s) with this change.

The syntax of the ... is a commit range. grab all commits from start (exclude) to the last one. If you want a single commit use a single SHA-1

enter image description here


Read out the full git cherry-pick documentation for all the options you can use

Community
  • 1
  • 1
CodeWizard
  • 128,036
  • 21
  • 144
  • 167
  • Apologies, I fudged up the explanation a bit, and have edited the question to clarify the situation. Would cherry-pick still apply? I don't really see how it does. – Kumalh Apr 26 '16 at 12:07
0

In this case with only small number of commits, git cherry-pick as @CodeWizard suggested, is the easiest way. But if you have lots more commits, that might not be practical. Here's a solution using git rebase --onto.

# create feature2 and rebase it onto A
git checkout -b feature2 D
git rebase --onto A B feature2

# move the feature branch to be just on B
git checkout feature
git reset --hard B

https://git-scm.com/docs/git-rebase

Mort
  • 3,379
  • 1
  • 25
  • 40