297

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.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
Sebastian Schneider
  • 4,896
  • 3
  • 20
  • 47
  • 3
    Possible duplicate of [Insert a commit before the root commit in Git?](http://stackoverflow.com/questions/645450/insert-a-commit-before-the-root-commit-in-git) – msw Dec 05 '15 at 01:39
  • 1
    Why do you want to do this? You can call the empty set "Fred", but it is still the empty set. – msw Dec 05 '15 at 01:40
  • If you tell us more about *why* you want to do this, it might help us come up with good answers. – willoller Dec 05 '15 at 01:41
  • Everything you will make with git will be _local_ until you push it upstream to the GitHub server with `git push origin _branch_`. – Joe Nov 13 '20 at 18:10
  • 3
    One thing to note here: there is, technically, no such thing as an *empty branch*. What you want is an *orphan* branch with an *empty index*, which is what the accepted answer produces. This might seem like a pointless technical quibble, but in fact it's a very point-ful (if that's a word) technical quibble: Git's functionality is essentially nothing *but* technical quibbles, so you need to be very precise with your technical quibbling, when Git-ing. – torek Aug 24 '22 at 19:17

5 Answers5

658

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
Alecto Irene Perez
  • 10,321
  • 23
  • 46
C. Augusto Proiete
  • 24,684
  • 2
  • 63
  • 91
  • 7
    Hi, after creating empty-branch github repository is not showing any file difference when i'm try to creating pull-request from other branch. Can you tell me how to resolve this. – Raghavendra S Nov 14 '19 at 15:31
  • 1
    @RaghavendraS See my answer. I had the same issue. – Greg M. Krsak May 05 '20 at 04:39
  • 3
    Perhaps some people are having an issue with this accepted answer because it is missing a `git add .` step after you have removed all files and before you commit? – SCoyle May 20 '20 at 10:10
  • `git rm -rf .` delete file from locals – Talha Anwar Dec 17 '21 at 12:42
  • 1
    the problem is that since its an orphan you cannot use it for a PR. this basically doesn't answer the question: how to make an empty branch *on github*, not in git. – Erik Aronesty Mar 07 '22 at 19:15
28

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

thisismydesign
  • 21,553
  • 9
  • 123
  • 126
7

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.

Community
  • 1
  • 1
houtanb
  • 3,852
  • 20
  • 21
3

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.

Greg M. Krsak
  • 2,102
  • 1
  • 18
  • 28
  • 5
    If your branch has a base commit, by definition, it's not a _empty_ branch. What are the problem(s) you had when creating an orphan branch? – C. Augusto Proiete Jun 01 '20 at 22:17
0

The accepted answer is indeed the way to go. However if you already have other branches that cannot be merged to de newly created fresh development branch, my personal favorite solution is to perform a rebase, execute the following on the branch that needs to merge to develop:

git rebase develop

That way the initial commit will be the first commit on the branch to merge as well. There is of course a caveat :) You are rewriting history on the branch by performing a rebase. So you will have to force push the branch to origin:

git push -f

Others that also use the branch, will be unable to pull now, so they will have to perform the rebase as well, locally, after fetching develop. Assuming they are on the branch that needs to be rebased:

git fetch
git rebase origin/develop