简体   繁体   中英

How do I create a new branch cleaning up the commits from another branch?

I have two branches, master and feature .

I finished working on feature , so this branch have a lot of commits, but I don't want to publish it as is. I want to create a branch called feature_clean with all the modifications from feature but with better commits.

I tried the following:

git checkout -b feature_clean master
git checkout feature
git rebase --interactive feature_clean
# reorganize commits etc, save and close editor

, and this created feature_clean correctly BUT it also modified feature . In fact, both branches were equal.

What did I do wrong? I want to keep feature as is for now (I'll delete it later, after feature_clean is appropriately tested and approved).

you have your rebase command wrong;) you are telling git to rebase the current branch ( HEAD , in that case feature ) on top of feature_clean . I think what you actually want to do is:

git checkout -b feature_clean feature
git rebase -i master

ie replay commits from feature_clean on master . feature will still point to the old commits.

git rebase master is shorthand for git rebase --onto master master HEAD : take all commits between master and HEAD (reachable from HEAD , but not from master ) and stick them onto master

  1. Checkout your feature branch
    git checkout feature

  2. Create your new feature_clean branch
    git checkout -b feature_clean

  3. Rebase that branch onto where you split off of master
    git rebase -i --onto shaOfSplit shaOfSplit feature_clean

  4. Do your interactive rebase

At the end of your rebase you will have two branches broken off from the same point. feature_clean will be a rebased version of feature

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