nobe4
nobe4

Reputation: 2842

Git clean history and preserve commits after merge

In a project our git log was like

 1--2--3----------------------------12--        (master)
        \-4--5--------8---9------11-/  \        (branch1)
              \--6-------/---10-/       \       (branch2)
                  \-7-------/            \      (branch3)
                                          \-13  (newbranch) 

and I want to have :

1--2--3--4--5--6--7--8--9--10--11--12--         (master)
                                     \-13       (newbranch) 

That is to delete all branches but preserve the history and the commits (for further references).

Thanks in advance.

Upvotes: 0

Views: 505

Answers (4)

Stas Dashkovsky
Stas Dashkovsky

Reputation: 111

You can check git log master and git log newbranch - that will looks exactly how you want. If you really want to remove all branches and merges and do history plain, it's better to use git rebase --root master.

Upvotes: 0

budhajeewa
budhajeewa

Reputation: 2408

I created a screencast answering this. Hope it'd help you.

It demonstrates creating a straight Git tree based on a complex one, using SmartGit Git client.

https://www.youtube.com/watch?v=f-1u_MTr2Uw

Upvotes: 0

Dan
Dan

Reputation: 4199

I would make this a comment but I don't have enough reputation yet. Can you clarify something - are commits 9 through 12 merge commits? If yes, why would you want to include merge commits in the linear history? Isn't the point of a linear history to omit merge commits?

Edit: Regardless, try this:

1) Figure out the commit hash of commit 1. Let's say it's 5678.

2) git checkout master. Then execute git rebase -i 5678. You'll get a prompt. Reorder the commits 1 through 12. Then save and exit the editor.

3) git checkout newbranch. Then execute git rebase master.

Rewriting history is always risky, so I'd make sure to have another branch pointing to master before I tried this (origin/master or some other local branch), and that way if things go awry, I can git reset --hard <pointer-to-previous-master>. Same goes for newbranch.

After you're done and everything looks good on master and newbranch, you can delete branches 1-3 using git branch -D.

Upvotes: 4

Kazuki Sakamoto
Kazuki Sakamoto

Reputation: 14009

$ git checkout -b new/master 3  (3 should be replaced with the commit sha1 of 3)
$ git cherry-pick 4             (4 should be replaced with the commit sha1 of 4)
$ git cherry-pick 5
$ git cherry-pick 6
...
$ git cherry-pick 12

$ git checkout -b new/newbranch
$ git cherry-pick 13

$ git branch -D master
$ git branch -D branch1
$ git branch -D branch2
$ git branch -D branch3
$ git branch -D newbranch

$ git branch -m new/master master
$ git branch -m new/newbranch newbranch

Upvotes: 0

Related Questions