Git orphan branches

Vinoth
TarkaLabs TIL
Published in
1 min readDec 5, 2019

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).

--

--

Vinoth
TarkaLabs TIL

Programmer by day and by night and by full moon and all the other time