kamwitsta
kamwitsta

Reputation: 406

Split one branch into two in Git

I have a project with a completely linear history:

master    A – B – C – D – E – F – G – H

I'd like to split it into two: develop with most of the commits, and master, into which develop merges only at milestones:

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

Upvotes: 0

Views: 65

Answers (1)

matt
matt

Reputation: 534977

Your diagram makes no sense; you cannot move D, for example, the way you have drawn it. Moreover, if no one else works on master, it doesn't make sense to make a separate merge commit on master as you have drawn it, because it can just be fast-forwarded to follow develop.

Nevertheless, we can do the general thing you describe, by forcing the existence of merge commits at various stages along the history. OK, so master is currently at H. Here we go:

  1. Make a branch develop but do not check it out.

  2. You are still on master. Reset --hard to A (using the SHA number). So now master is at A, and develop goes A-B-C-D-E-F-G-H.

  3. You are still on master. Merge D --no-ff (using its SHA) to get a nice merge commit. Merge anywhere else along develop that you think is a meaningful staging point.

  4. Checkout develop and keep working.

I will demonstrate. We have this:

* ddb5eeb (HEAD -> master) f
* d995c3c e
* a1f5432 d
* b6c9729 c
* b15090c b
* ee1de04 a

Now:

$ git branch develop
$ git reset --hard ee1d
$ git merge --no-ff a1f5
$ git merge --no-ff develop
$ git checkout develop

And we now have this:

*   2ad4bb2 (master) Merge branch 'develop'
|\  
| * ddb5eeb (HEAD -> develop) f
| * d995c3c e
* |   c6f43e4 Merge commit 'a1f5'
|\ \  
| |/  
| * a1f5432 d
| * b6c9729 c
| * b15090c b
|/  
* ee1de04 a

...which is just the sort of thing you said you wanted.

Upvotes: 3

Related Questions