We are using a git repository to store our project. We have our branches departing from the original branch. But now we want to create a small new project to track some documentation. For that we would want to create a new empty branch to start storing our files, and I would want other users of the network to clone that branch.
How can we do that?
I tried some things, but they didnt work.
$ mkdir proj_doc; cd proj_doc
$ git init
$ git add .
$ git commit -m 'first commit'
$ git br proj_doc
$ git co proj_doc
$ git br -d master
$ git push origin proj_doc
It seems to push the branch ok, but when I do a fetch or pull, it downloads information from other branches, and then I also get some extra files from other projects. What's the best solution?
Make an empty new branch like this:
If your proj-doc files are already in a commit under a single subdir you can make the new branch this way:
which might be more convenient than
git branch --orphan
if that would leave you with a lot ofgit rm
andgit mv
ing to do.Try
and see if that helps with your fetching-too-much problem. Also if you really only want to fetch a single branch it's safest to just specify it on the commandline.
The correct answer is to create an orphan branch. I explain how to do this in detail on my blog: http://sidja.in/post/62663941071
if git version does not have --orphan option, this method should be use;
After doing some works
You can create a branch as an orphan:
This will create a new branch with no parents. Then, you can clear the working directory with:
and add the documentation files, commit them and push them up to github.
A pull or fetch will always update the local information about all the remote branches. If you only want to pull/fetch the information for a single remote branch, you need to specify it.
Let's say you have a
master
branch with files/directories:Step by step how to make an empty branch:
git checkout —orphan new_branch_name
ls -la |awk '{print $9}' |grep -v git |xargs -I _ rm -rf ./_
git rm -rf .
touch new_file
git add new_file
git commit -m 'added first file in the new branch'
git push origin new_branch_name
In step 2, we simply remove all the files locally to avoid confusion with the files on your new branch and those ones you keep in
master
branch. Then, we unlink all those files in step 3. Finally, step 4 and after are working with our new empty branch.Once you're done, you can easily switch between your branches: