avp::ptr weblog

Everything that is in our mind.

Archive for the ‘orphan’ tag

How to create an independent branch in git?

without comments

While working with a version control system like git it is sometimes a good idea to create an independent branch. This article shows how to accomplish that. In this example I want to hold onto the documentation in a separate branch.
I assume, that you already created a repository and added various commits onto you master branch.

Prepare your ignore list
Now its time to prepare for the repository. First, edit your .gitignore file and exclude the doc/ folder which you will be creating in the next step.

Create an orphan branch
Create a new independent branch with the git checkout option --orphan. The option is available since Git 1.7.2. Initially, you will have to remove the files that have been created in the working directory, but right after the branch is independent.
Now you can create a doc/ folder and create documentation files. In the end you can commit them to the repository as usual.

git checkout --orphan documentation
git rm -rf .
mkdir doc
cd doc
// Do work.
// git add your files to the doc/ folder
git commit -m "Added documentation files."

Checkout the master branch
To checkout the master branch while still being able to list the documentation files do the following.

git checkout master && git merge --no-commit documentation
git reset master

The files added to the documentation branch earlier will appear in the working directory visible to the master branch. Thanks to the .gitgnore file they will not be listed as untracked files.

The initial discussion on an unrelated branch can be found on stackoverflow.com. Thanks to hillutcovo and phord for their input!

Written by tobi

October 9th, 2011 at 5:16 pm

Posted in Uncategorized

Tagged with , , ,