Skip to content

Instantly share code, notes, and snippets.

@Prasad-Medisetti
Forked from ozh/new empty git branch.md
Last active October 25, 2023 09:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Prasad-Medisetti/7046ed787a58eca5467c5b26f78f5d92 to your computer and use it in GitHub Desktop.
Save Prasad-Medisetti/7046ed787a58eca5467c5b26f78f5d92 to your computer and use it in GitHub Desktop.
Create a new empty branch in Git

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

$ git checkout --orphan NEWBRANCH
$ git rm -rf .

--orphan creates a new branch, but it starts without any commit. After running the above command you are on a new branch "NEWBRANCH", and the first commit you create from this state will start a new history without any ancestry.

You can then start adding files and commit them and they will live in their own branch. If you take a look at the log, you will see that it is isolated from the original log.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment