Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create empty branch on GitHub

Tags:

git

github

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 and I don't want to have its commit history.

The only method I found is to create a local --orphan branch.

like image 504
Sebastian Schneider Avatar asked Dec 05 '15 01:12

Sebastian Schneider


People also ask

Can we create empty branch in git?

As of Git 2.23, you can use git switch --orphan <new branch> to create an empty branch with no history.

How do I create an orphan branch in GitHub?

Creating an Orphan Branch In your project repository an orphan branch can be created with the following Terminal command. In my case, I want my new branch to be named landing-page . With the git checkout --orphan landing-page command, a landing-page branch is created and checked out.

Can you create a branch in GitHub?

You can create a branch in different ways on GitHub. Note: You can only create a branch in a repository to which you have push access.


2 Answers

November 2021 Update: As of git version 2.27, you can now use git switch --orphan <new branch> to create an empty branch with no history.

Unlike git checkout --orphan <new branch>, this branch won't have any files from your current branch (save for those which git doesn't track).

This should be the preferred way to create empty branches with no prior history.

Once you actually have commits on this branch, it can be pushed to github via git push -u origin <branch name>:

git switch --orphan <new branch> git commit --allow-empty -m "Initial commit on orphan branch" git push -u origin <new branch> 

Original answer:

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 
like image 134
C. Augusto Proiete Avatar answered Oct 08 '22 18:10

C. Augusto Proiete


--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.

like image 31
thisismydesign Avatar answered Oct 08 '22 17:10

thisismydesign