CubanTurin CubanTurin - 3 years ago 148
Git Question

Splitting git history into branches

We use Git for out repo. I've no idea how but my team's latest history ended up like this:

0 master
1-2-3-4-5-6-7 branch1 & branch2

Notice how (thankfully) nobody has merged this with our master branch yet. Now, I want to split that to end up like this:

5-6-7 branch2
/ \
0 - - - (*) - - (*) master
\ /
1-2-3-4 branch1

I'm not experienced with Git and I'd like to know how to achieve that

Answer Source

Assuming both branch1 and branch2 are pointing at 7, the safer option is to create another branch n_branch1 and do the following:

git checkout <sha_4>
# you'll now be in a detached head
git checkout -b n_branch1   
# Now go to master and merge n_branch1
git checkout master
git merge n_branch1
# Once branch1 commits are merged, apply branch2 on top of them.
git checkout branch2
git rebase master

Note: You could also reuse branch1 by reverting it to 4 (see here).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download