Create Orphan Branch
This guide will show you how to create a new, empty Git branch (called an orphan branch) that has no history from your current branch. This is useful for things like deploying to GitHub Pages (gh-pages
), maintaining documentation, or creating a fresh start.
π§ What is an Orphan Branch?β
An orphan branch is a new branch that starts with no previous commits or history. Itβs like starting with a blank project, even though you're in the same Git repository.
β Step-by-Step Instructionsβ
1. Open your terminal and navigate to your project folder.β
cd your-project-folder
2. Create a new orphan branchβ
Replace new-branch-name
with the name you want (for example: gh-pages
, docs
, clean-start
, etc.)
git switch --orphan new-branch-name
π This will switch you to a new branch without any commit history.
3. (Optional) Delete all existing files from the working directoryβ
You may still see your old files. If you want to start fresh, remove them:
git rm -rf .
β οΈ This only removes files from the orphan branch, not your main branch.
4. Make an initial commitβ
Create an empty first commit to initialize the branch:
git commit --allow-empty -m "Initial commit"
5. Push the branch to GitHub (or other remote)β
Replace origin
with your remote name (default is usually origin
) and new-branch-name
with your branch name.
git push -u origin new-branch-name
π This pushes your new branch and sets it to track the remote version.
π§Ό Youβre Done!β
You now have a clean branch with no history, ready for whatever purpose you need β like documentation, a demo site, or a different version of your project.
π‘ Tipsβ
-
Switch back to your main branch anytime with:
git switch main
-
List all branches (local and remote):
git branch -a
π Example: Creating a gh-pages
Branch for GitHub Pagesβ
git switch --orphan gh-pages
git rm -rf .
git commit --allow-empty -m "Initial commit"
git push -u origin gh-pages
You can now add your static site files and push again to publish.
Happy coding! π