29 Moving Branches

Sometimes you need to move branches around to reorganize them. For example, you have a branch where you’re working on a future version of your software. During the rewrite, your team realizes that the better-widget feature can be released as a minor version of your software instead of being part of the next major version. This is where you can move a branch to make your repository more sane.

You use git rebase --onto to move branches. Like a normal git rebase, Git replays the commits from one branch against another. The difference is that Git takes the branch you’re rebasing, and instead of replaying it against another branch, it moves it to an entirely different one.

The syntax is more verbose than a simple git rebase that requires only one additional parameter. git rebase --onto takes three: the first is the branch you’re rebasing onto, the second is the branch you’re rebasing from, and the third is the branch you want to move.

What To Do...
  • Move branch better-widget from next-release to master.

    This is the current state of the repository: the next-release branch was created from the master branch, and the better-widget branch was created from the next-release branch.

     
    o--master--o
     
     
    o--next-release--o
     
     
    o--better-widget--o

    The command to move the branch is a specialized version of git rebase:

     
    prompt>​ git rebase --onto master next-release better-widget

    After running this command, use this:

     
    o--better-widget--o
     
    /
     
    o--master--o
     
     
    o--next-release--o

    next-release is moved to the end of master, so if there were more commits in master than were in next-release, better-widget is placed on top of those. Here’s an example:

     
    o--master--o--o--o
     
     
    o--next-release--o
     
     
    o--better-widget--o

    After running git rebase --onto, use this:

     
    o--better-widget--o
     
    /
     
    o--master--o--o--o
     
     
    o--next-release--o

Related Tasks

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.30.210