Git orphan branches
Recently I was looking to change the theme of my Jekyll hosted blog, and in Jekyll changing the theme is essentially pulling the new theme and replacing your existing _posts
directory with the theme’s _posts
directory.
For this I needed a way to replace everything in my repository except the _posts
folder. Enter orphan branches.
Orphan branches creates a branch without any of the commit history but just the files from the source branch.
git checkout --orphan branch-name
Once done, all the existing files will be staged in git. After discarding all the changes and pulling the desired theme’s repo and checking out my existing _posts
directory satisfied the requirement.
Orphan branches can also be used to erase the entire commit history of a git repository, simply checkout orphan and rename it to master (after deleting the original master branch).