1

I am currently working with this git tree/workflow

A [master]
 \
  B ------------- D [develop] 
   \             / 
    C1 -- C2 -- C3 -- C4 [feature1]

Now i wanted to create a new branch for the 2nd feature but i forgot to check out the new branch and i commited new code to the Feature1 branch.

A [master]
 \
  B ------------- D [develop] 
   \             / 
    C1 -- C2 -- C3 [feature2]
                 \
                  C4 [feature2]

So what i want to do now it to remove C4 form the feature 1 branche and add it to the feature 2 branche which is still non existent. C4 is not yet pushed to the remote, the rest of the project is.

Funonly
  • 283
  • 1
  • 13
  • 2
    Your ASCII graphs are unclear/ambiguous. Please read the remarks at the top of [this answer of mine](http://stackoverflow.com/questions/25488138/move-initial-commits-off-master-to-another-branch-in-git/25490288#25490288), and correct your graphs so we know exactly where your branches are pointing. Only then will people here be able to help you. – jub0bs Oct 07 '14 at 16:33

1 Answers1

1

Do the following:

  1. Make sure you have a clean working state (create a stash if you need to). At this stage, your repo still looks like this:

    A [master]
     \
      B ------------- D [develop] 
       \             / 
        C1 -- C2 -- C3 
                     \
                      C4 [feature1]
    
  2. Create and check out a branch called feature2 that points at the same commit as feature1 does:

    git checkout -b feature2 feature1
    

    Your repo will then look like this:

    A [master]
     \
      B ------------- D [develop] 
       \             / 
        C1 -- C2 -- C3
                     \
                      C4 [HEAD=feature2,feature1]
    
  3. Reset the feature1 branch by one commit:

    git checkout feature1
    git reset --hard HEAD^
    

    Your repo will then look like this:

    A [master]
     \
      B ------------- D [develop] 
       \             / 
        C1 -- C2 -- C3 [HEAD=feature1]
                     \
                      C4 [feature2]
    
  4. If you want to continue working on the feature2 branch, check it out (and also pop your last stash, if you created one):

    git checkout feature2
    

    Your repo will then look like this:

    A [master]
     \
      B ------------- D [develop] 
       \             / 
        C1 -- C2 -- C3 [feature1]
                     \
                      C4 [HEAD=feature2]
    
jub0bs
  • 60,866
  • 25
  • 183
  • 186