Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 687
hakunin Avatar asked Dec 08 '11 09:12

hakunin


People also ask

Did work on wrong git branch?

The easiest way is to check git log and note your commit hashes, checkout your right branch and do: git cherry-pick h4sh . Other way is to checkout the right branch and merge it the one which you worked, but then it'll merge the other changes as well, so it depends if you want to do that.

How do I undo a push to the wrong branch?

Make sure you are on the branch to which you have been committing. Use git log to check how many commits you want to roll back. Then undo the commits with git reset HEAD~N where “N” is the number of commits you want to undo. Then create a new branch and check it out in one go and add and commit your changes again.


1 Answers

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 
like image 118
reto Avatar answered Sep 22 '22 06:09

reto