0

I have one branch which has many unnecessary commits so I would like to branch from it to a version with clean history.

This is example:

Master:
commit1
commit2
commit3

BranchB:
commitB1
commitB2
commitB3
commitB4
commitB5
commitB6

After merge:
commit1
commit2
commit3
commitB1
commitB2
commitB3
commitB4
commitB5
commitB6

Instead of that I would like to have something like this:

commit1
commit2
commit3
commitB6

Is it possible to clone all the code from branch B (state after commit B6) and copy it to a new branch C with a new commit message?

Cristiano
  • 3,099
  • 10
  • 45
  • 67
  • possible duplicate of [How to use git-merge --squash?](http://stackoverflow.com/questions/5308816/how-to-use-git-merge-squash) – Andrew C Dec 08 '14 at 01:34

3 Answers3

0
git checkout master
git checkout -b branchc
git merge --no-ff branchb

then the merge commit incorporates changes of branchb from b1-b6 on top of master.

DRC
  • 4,898
  • 2
  • 21
  • 35
  • Thanks! On more question. How to have different commit message than from b6? I would like to have a one commit message which would include short version of messages from b1 to b6. – Cristiano Dec 08 '14 at 01:14
  • @Cristiano actually I was right even if I was wrong, the merge commit is not b6, but a new commit, modifying the answer – DRC Dec 08 '14 at 01:30
  • Thank you but it seems it's not working. I still see old log messages. – Cristiano Dec 08 '14 at 02:17
  • @Cristiano then probably you need a --squash instead of no-ff as pointed out by Andrew C , see the difference [here](https://stackoverflow.com/questions/18388813/whats-the-difference-between-git-merge-no-ff-and-git-merge-squash) – DRC Dec 08 '14 at 02:17
0

Create a new branch from current master

git checkout -b BranchC

Do a rebase interactive and remove the commits you don't want

git rebase -i commit3

In the rebase interactive editor it will look like this

pick commit3
pick commitB1
pick commitB2
pick commitB3
pick commitB4
pick commitB5
pick commitB6

delete the commits you don't want. The editor might look like this than.

pick commit3
pick commitB6

save and quit.

Hopefully commitB6 does not depend on changes of commitB1..commitB5. Otherwise you have to resolve the conflicts.

René Link
  • 48,224
  • 13
  • 108
  • 140
-1

You can also do this, a cherry-pick

git checkout master
git checkout -b branchc
git cherry-pick commitB6
Narasimha
  • 759
  • 6
  • 8