Creating a new empty branch for a new project
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 didn't 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?
Solution 1:
You can create a branch as an orphan:
git checkout --orphan <branchname>
This will create a new branch with no parents. Then, you can clear the working directory with:
git rm --cached -r .
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.
Solution 2:
The correct answer is to create an orphan branch. I explain how to do this in detail on my blog.(Archived link)
...
Before starting, upgrade to the latest version of GIT. To make sure you’re running the latest version, run
which git
If it spits out an old version, you may need to augment your PATH with the folder containing the version you just installed.
Ok, we’re ready. After doing a cd into the folder containing your git checkout, create an orphan branch. For this example, I’ll name the branch “mybranch”.
git checkout --orphan mybranch
Delete everything in the orphan branch
git rm -rf .
Make some changes
vi README.txt
Add and commit the changes
git add README.txt git commit -m "Adding readme file"
That’s it. If you run
git log
you’ll notice that the commit history starts from scratch. To switch back to your master branch, just run
git checkout master
You can return to the orphan branch by running
git checkout mybranch