2

I'm trying to do a similar thing to this: Creating GitHub repository with only a subset of a local repository's history

I currently have all my commits on master and now I want to make a branch called release that only has a single commit of all past commits and no history of the old commits.

I tried using the method in that link but doing so caused any further merges into release to not merge automatically, which is somewhat annoying. (The error it gives is Squash commit -- not updating HEAD. Automatic merge failed; fix conflicts and then commit the result.) Is there a different way of doing it that can still merge automatically?

I know the easiest method would be to create a separate repo but, ideally, I'd rather just use a branch.

I want it to end up looking something like this:

v1 is a merge from commit 2, v2 is a merge from commit 5, etc.

[release]:

  • v2
  • v1

[master]:

  • commit 5
  • commit 4
  • commit 3
  • commit 2
  • initial

I'm new to git so sorry if this is really obvious! Thanks!

Community
  • 1
  • 1
Nick
  • 58
  • 7
  • How are you doing 'further merges'? – ellotheth May 26 '12 at 22:46
  • Why not just merge master into release for each version and use `git merge --no-ff` to always only create one (merge) commit in the release branch for each version. – Mattias Wadman May 27 '12 at 08:43
  • @MattiasWadman, it does create a separate branch structure doing it that way but when you push the code to say, Github, the previous commits from master are still visible. If I create the release branch off initial, then use `git merge --squash` it does work and do what I want but it still cant merge automatically. – Nick May 27 '12 at 13:33
  • If you want that each commit to the release branch is the exact same tree as in master at that point you can actually do something like this when having the release branch checked out `git read-tree -u --reset master && git commit -m v1`. Now there will be a "v1" commit in the release branch point to the exact same tree as the top commit in master. – Mattias Wadman May 27 '12 at 14:18
  • But is there a good reason why you want to hide the commit messages in the master branch? maybe it is better to rebase and cleanup your commits in master and then do a proper merge instead? – Mattias Wadman May 27 '12 at 14:20
  • @MattiasWadman, that works perfectly! Thanks! You're right though, I probably should have cleaner commits, this was my first git project so I didn't have a proper branching structure and just commited everything on master. Now I've done a bit more reading on the subject and will use a proper branching technique in my next project! – Nick May 27 '12 at 14:55
  • Nice, i converted my comment into an answer. Also read about interactive rebase if you want to know how to cleanup your commits. – Mattias Wadman May 27 '12 at 15:02

1 Answers1

3

If you want to do a commit in the release branch that will have the exact same tree as the top commit in the master branch you can do:

# make sure we are on the release branch
git checkout release
# populate index and working tree with tree from top master commit 
git read-tree -u --reset master
# commit it as "v1"
git commit -m v1
Mattias Wadman
  • 11,172
  • 2
  • 42
  • 57