简体   繁体   中英

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.)

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

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.

what about creating a patch, checkout to the develop branch and apply the patch?

git checkout new-feature2

git format-patch new-feature

git checkout develop

git am name-of-the-patch.patch

你也可以使用git diffgit apply

git diff new-feature..new-feature2 | git apply -

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM