git rebase --onto results on single commit

梦想的初衷 提交于 2019-12-12 03:29:15

问题


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.


回答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


来源:https://stackoverflow.com/questions/19181665/git-rebase-onto-results-on-single-commit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!