Home > Software engineering >  I should have checked out from another branch in Git, how do I fix it?
I should have checked out from another branch in Git, how do I fix it?

Time:11-19

I created a feature branch from master but I should have done it from develop branch how can I fix it?

For visualization I've done this:

          D---E---F develop
         /
A---B---C master
         \
          G---H feature

but I should have done this:

                    G---H feature
                   /
          D---E---F develop
         /
A---B---C master

I haven't tried anything, I am not sure how to approach this

CodePudding user response:

This is a text-book example of using git rebase, assuming you haven't brushed feature yet.

git checkout feature
git rebase develop

CodePudding user response:

This is simply done by re-creating all commits of feature on top of develop. A command which can do this, is git rebase:

git rebase --onto develop master feature
  • Take all commits between head commit of master (exclusive) and head commit of feature (inclusive)
  • Recreate them on top of the develop branch

Note that re-creating commits are independent of the old commits and they will be assigned a new commit id/hash. If the history is already shared with others, it shouldn't be altered, unless absolutely necessary. Rewritten history also needs to be force-pushed, because the ref on the remote server cannot be fast-forwarded.

To get the changes of develop without rewriting history, simply use git merge to bring both histories together:

git checkout feature
git merge develop
  •  Tags:  
  • git
  • Related