0

I want to create a branch out of master but I need this branch to have an empty tree. After a bit of researching I've come up with the following situation:

  • Master branch have a single commit with a dummy file.
  • I checkout a new branch
  • I remove all files and commit
  • I create a new commit with --allow-empty

The following commands should get you up to that:

$ git init  
$ touch dummy  
$ git add .  
$ git commit -m 'initial commit'  
$ git checkout -b new-branch  
$ git rm -rf .  
$ git commit -m 'clean up tree'  
$ git commit --allow-empty -m 'new branch initial commit'

Now I want to get rid of 'clean up tree' commit. I'm trying to use rebase --onto like

$ git rebase --onto HEAD~2 HEAD^  

But I end up with a single commit ('initial commit') and all refs on it (HEAD, master, new-branch). And if I checkout into new-branch, dummy file is back.

Where did my 'new branch initial commit' went? What I'm a missing?

Obs.: I'm doing this because I want to relate those branches but I don't want to keep the files from the parent commit.

Eduardo Mello
  • 925
  • 3
  • 15
  • 32
  • So i've got to learn --keep-empty flag for git rebase and it seems to have kept 'new branch initial commit' as I wanted. But if I list the files 'dummy' is back. – Eduardo Mello Oct 04 '13 at 15:20

1 Answers1

1
true | git mktree | xargs git commit-tree -p master | xargs git branch new-branch

which is the quickest one-liner for

emptytree=$(git mktree </dev/null)
emptycommit=$(git commit-tree -p master $emptytree </dev/null)
git branch new-branch $emptycommit
jthill
  • 55,082
  • 5
  • 77
  • 137
  • Thank you! I was going down that path after I read this http://git-scm.com/book/en/Git-Internals-Git-Objects and then found this question http://stackoverflow.com/questions/9765453/gits-semi-secret-empty-tree. The second link explains the /dev/null tree you're doing there. – Eduardo Mello Oct 05 '13 at 02:51
  • Last thing: you forget a / on the second command, on – Eduardo Mello Oct 05 '13 at 02:53
  • I understand everything except one thing: what is the true doing on the on-liner? I figure it's taking the /dev/null place, but how that goes? thank you. – Eduardo Mello Oct 05 '13 at 03:28
  • 1
    `true`'s output is empty so piping from it is like reading from `/dev/null`. You can save even more keystrokes with `git mktree<&-|etc` but that just infuriates people when I suggest it :-) --- and thanks for the fixie, applied – jthill Oct 05 '13 at 03:38