docwhat's avatardocwhat's blog

Git Tip: empty branch

Ever wanted to make an empty branch (with no history in it) in git? It’s not hard…

Why would you want to do this? Well, for example, I have a new project in github. I’m trying to implement it in several ways, jRuby, Java, Qt, etc. Having separate branches is really handy.

Another reason you might want to do this is for upstream tracking. I used this to cold-start a branch where I commit all the changes of an upstream project and then merge them into my customized branch. Git makes tracking an upstream project much easier.

Anyway, enough of that. Here’s how to do it:

# WARNING: Make sure you've committed all your work...
#
# Point git at a new branch:
git symbolic-ref HEAD refs/heads/yournewbranch

# Remove the index file so it doesn't know about the files
# that are already checked out:
rm .git/index

# Clean up all the files that were checked out before:
git clean -xfd

# Done!
Edit on GitHub