0

Currently i have my remote develop branch with commits like:

C1 -> C2 -> C3 -> C4 -> C5 -> C6

(Cx as commit x)

I need to divide these commits to new branches, so it will look like this:

Develop branch: C1 C2 C6

NewBranchTwo: C3 C5

NewBranchThree: C4

(NewBranchTwo and NewBranchThree does not need to become remote)

How can i achieve this?

2 Answers2

2

git rebase is your friend.

One easy way to accomplish this is to use git branch to produce three identical branches (Develop, NewBranchTwo, NewBranchThree), which you then edit down using

git rebase --interactive C1

...replacing C1 with the commit's hash or other ref. In interactive mode, git will first pop open a text editor where it shows you the entire history, and where commits wil be applied in order from top to bottom according to any deletions or reorderings you make in the editor. You could easily remove C3 through C5 for Develop, and make corresponding edits to the other branches.

You can also skip the interactive mode and use git reset and git rebase --onto to move C6 onto C2 and name that Develop, and the same with C3 onto your upstream head and C5 onto that (etc). Without knowing the actual hashes or parent of C1, I'll leave the actual commands there to the git rebase documentation.

(You put in your tags, but if you're trying to divide commits, git revert won't help: it records new commits that undo existing commits without erasing them. If you want history to look as it does, rather than C1 C2 C3 C4 C5 C6 rev-C5 rev-C4 rev-C3, you'll need to rebase or otherwise edit history.)


As you're probably aware, any time you're adjusting commits that have already happened, you change their hashes and may make it difficult for anyone who has branched already. Hopefully you haven't pushed your remote Develop branch yet!

Jeff Bowman
  • 90,959
  • 16
  • 217
  • 251
0

What I ended up doing:

Develop: C1 C2 C3 C4 C5 C6 rev-C6 rev-C5 rev-C4 rev-C3 rev-C2 rev-C1

From develop i created NewBranchTwo and NewBranchThree - so all three had all commits reverted.

Then on each branch: Develop branch: rev-rev-C1 rev-rev-C2 rev-rev-C6

NewBranchTwo: rev-rev-C3 rev-rev-C5

NewBranchThree: rev-rev-C4

So basically I reverted all commits, created the two new branches, then on each branch i reverted the reverted commits i needed.