Orphan branches in Git are rare as hens’ teeth though I’m writing from my personal experience. What, how and why are created orphaned branches will be the topics for today.
Prerequisites
- Git
Solution
Orphan branch is a Git branch that has no parents (you don’t say) or git history if you will. Starts with a completely different / new root commit.
Although the proper way to create an orphaned branch is:
git switch --orphan <orphaned_branch_name>
It will delete ALL files from the working directory except the ones ignored by .gitignore
.
Another less risky way with all files intact:
git checkout --orphan <orphaned_branch_name>
To commit and push this branch as empty run:
git commit --allow-empty -m "init commit orphan"
git push origin <orphaned_branch_name>
Use cases:
- Deleting your Git repo commit history for various reasons including exposing sensitive data.
- Combining multiple repositories as one.
- Separate static website documentation from the actual source code.
Conclusion
To find more neat Git commands and hacks, simply browse the Git category. Feel free to leave a comment below and if you find this tutorial useful, follow our official channel on Telegram.