2

An explanation is needed. So i worked on feature branch (lets call it feature/feature1), commit and push to remote.

git commit -m "commit feature1"
git push origin feature/feature1

Then I started working on a new feature, but instead of checking out to develop, pulling and creating new feature branch, I continued working on feature/feature1 branch.

I don't want to discard changes, how can I do in a way to create new branch and moving all new changes from feature/feature1 branch to the new created branch? Thanx.

chepner
  • 497,756
  • 71
  • 530
  • 681
Malloc
  • 15,434
  • 34
  • 105
  • 192

2 Answers2

1

You can use the --onto parameter of rebase to move some changes from one branch to another. There's an example of how to do that in the documentation, the section that starts with

Here is how you would transplant a topic branch based on one branch to another, to pretend that you forked the topic branch from the latter branch, using rebase --onto. ...

Your commands will probably looks something like this:

git branch feature/feature2
git reset --hard <sha1 of last commit of feature 1>
git rebase --onto <where you want branch to start> feature/feature1 feature/feature2 
Roman
  • 19,581
  • 6
  • 68
  • 84
  • Hi, `git branch -M feature/feature2` has deleted `feature/feature1` branch!! This is not what I was expecting. – Malloc Feb 07 '13 at 14:56
  • @Malloc It didn't delete it, it renamed it. I added an explanation of what happens. I also realized that wRAR's approach to shuffling branches around is probably more straight forward, so I modified my original answer to include that. – Roman Feb 07 '13 at 15:36
0

If you want to make a new branch containing the current commit, you should do git branch newbranchname. This will create a new branch but won't change the old one. If you want to remove new commits from the old branch, if that branch wasn't published yet, you may checkout it and do git reset --hard lastcommitid.

wRAR
  • 25,009
  • 4
  • 84
  • 97