0

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.

4
  • As I already made changes in a new branch (from master after the 12th commit) I prefer not to have to touch it. Rewriting history so that it be linear (as you want it) will change the IDs of commits 8-12 and of all the commits that come after that. Commented Dec 15, 2014 at 10:08
  • ok, so with changing the commits ID ? question edited Commented Dec 15, 2014 at 10:09
  • 1
    WHy not just delete branch1/2/3? What are you trying to gain by making it one line? Commented Dec 19, 2014 at 2:37
  • I wanted to clear up the history and delete unused branches (the history is a mess) Commented Dec 19, 2014 at 7:39

4 Answers 4

4
+50

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.

Sign up to request clarification or add additional context in comments.

2 Comments

9-11 are merge commits but I thought it wasn't useless to include them inside the linear history. I'll try this, I'll let you know.
seems that your solution worked (even if I got a lot of manual merge to do, Thanks a lot. Have a good christmas !
0
$ 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 

2 Comments

How to know the order of commits? sorting by date? If so, you can write a little shell script to generate a shell script like this answer.
I think so, but I wanted to know if it was possible to do it with only a git command or two ...
0

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

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.