Skip to content

Instantly share code, notes, and snippets.

@ysqi
Forked from j8/git_empty_branch
Created September 4, 2019 16:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ysqi/34a2bc1c372504c527479cf198ea94a8 to your computer and use it in GitHub Desktop.
Save ysqi/34a2bc1c372504c527479cf198ea94a8 to your computer and use it in GitHub Desktop.
Create new branch with empty folder structure
You can create a new empty branch like this:
$ git checkout --orphan NEWBRANCH
--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.
The --orphan command keeps the index and the working tree files intact in order to make it convenient for creating a new history whose trees resemble the ones from the original branch.
Since you want to create a new empty branch that has nothing to do with the original branch, you can delete all files in the new working directory:
$ git rm -rf .
Now you can 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.
Using the checkout command you can switch back and forth between the different branches like this:
$ git checkout master (back at the master branch)
$ git checkout NEWBRANCH (back at the new isolated branch)
You need to run git version 1.7.2 or higher in order for the --orphan option to be supported.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment