40

When I create branch in git, all the created files are added to the new branch.

How can I create a branch without adding all the existing files?

Chris Johnsen
  • 214,407
  • 26
  • 209
  • 186
vinyuri
  • 723
  • 2
  • 8
  • 11
  • 2
    If you are looking for a completely independent branch (no shared history and no common files), then this question is a duplicate of [“In git, is there a simple way of introducing an unrelated branch to a repository?”](http://stackoverflow.com/questions/1384325/in-git-is-there-a-simple-way-of-introducing-an-unrelated-branch-to-a-repository). – Chris Johnsen Feb 23 '11 at 05:05

3 Answers3

84
git checkout --orphan branchname
git rm -rf .

After doing that you can create, add, and commit new files and the resulting branch will have no common history with any other branches in your project (unless you merge them at some point).

Arrowmaster
  • 9,143
  • 2
  • 28
  • 25
2

The current answers are correct, you'd need an orphaned branch, but I would just add that coincidentally...

This is actually exactly how github.com lets users create Github Pages for their repos, thru an orphaned branch called gh-pages. The pretty steps are given and explained here:

https://help.github.com/articles/creating-project-pages-manually

Hope this helps!

unknownprotocol
  • 420
  • 3
  • 10
2

From the Git Book

git symbolic-ref HEAD refs/heads/newbranch 
rm .git/index 
git clean -fdx 
<do work> 
git add your files 
git commit -m 'Initial commit'
Guillermo Garza
  • 1,066
  • 1
  • 9
  • 8
  • Or did you want an orphan branch? In that case, see the answer by Arrowmaster. Though, the "--orphan" option was only added in 1.7.2. – Guillermo Garza Feb 25 '11 at 00:17