Git: created new branch from a wrong branch

I usually create new branch from develop

git checkout -b new-feature develop

then after the last commit I merge back to develop

git checkout develop
git merge new-feature

but this time I created new-feature2 brach from new-feature and now I cannot merge to develop.

Is there a way to switch new-feature2's parent to develop?

(The files I worked on were the same version as in develop so this should not require merging.)


Solution 1:

Note: Be sure to have a clean workspace before you begin. Read all messages git shows you. If in doubt, read more about the topic before starting.

You could rebase your feature over to the main base:

git checkout new-feature2  
git rebase --onto develop new-feature new-feature2
# rebase the stuff from new-feature to new-feature2 onto develop branch

or do it 'manually' by using cherry pick

git checkout develop
git log --oneline new-feature..new-feature2 
# for every commit call:
git cherry-pick <commit-id> # note, newer versions of cherry-pick allow multiple commits at once

Solution 2:

Have you seen interactive rebase?

git rebase -i develop

is a pretty simple solution–it'll show all your commits from that branch. Just delete the "pick" lines from the unwanted branch.