Create empty branch on GitHub

前端 未结 4 813
闹比i
闹比i 2020-12-22 16:31

I want to create a new GitHub branch, called release.

This branch needs to be empty! However, there is an existing branch with x commits

相关标签:
4条回答
  • 2020-12-22 16:37

    You can also follow the instructions here to create an empty commit at the root of your master branch. Then just create your release branch where that empty root commit is.

    0 讨论(0)
  • 2020-12-22 16:41

    --orphan is good for creating an empty branch locally, however, in order to push it or interact with other branches, you will need a commit.

    Creating a new commit on an orphan branch is not a good idea because you won't be able to interact with other branches. I.e.

    git checkout --orphan test
    git commit --allow-empty -m "init test branch"
    git merge master
    fatal: refusing to merge unrelated histories
    

    Instead, you should prefer creating a new branch from the first commit of master. If the commit is not empty you can add an empty commit before the first one, as explained by @houtanb.

    0 讨论(0)
  • 2020-12-22 16:49

    The accepted answer led me to some problems, so I did this:

    $ git branch
    * staging
    $ git branch master c74d99cf46f6ed23e742f2617e9908294b4a608b
    $ git checkout master
    Switched to branch 'master'
    

    And got what I wanted without and merge / pull-request issues. I just had to pick a base commit to create my second branch from.

    0 讨论(0)
  • 2020-12-22 16:58

    What's wrong with the --orphan option? If you want a branch that is empty and have no history, this is the way to go...

    git checkout --orphan empty-branch
    

    Then you can remove all the files you'll have in the staging area (so that they don't get committed):

    git rm -rf .
    

    At this point you have an empty branch, on your machine.

    Before you can push to GitHub (or any other Git repository), you will need at least one commit, even if it does not have any content on it (i.e. empty commit), as you cannot push an empty branch

    git commit --allow-empty -m "root commit"
    

    Finally, push it to the remote, and crack open a beer

    git push origin empty-branch
    
    0 讨论(0)
提交回复
热议问题