How to Create an Empty Orphan Git Branch
Hands-on guide on how to create an orphan Git branch without any parent, as a fresh new Git branch

Have you ever wanted to create a new, separate Git branch that doesn’t have any starting point from an existing branch?
One common use case is from the early days of GitHub, when you needed to create a separate empty branch called gh-pages. This branch was orphaned, meaning it had no relationship to the master or main branch, and was used to publish documentation sites to GitHub Pages.
This approach makes sense because you don’t want to upload your application source code from the main branch to GitHub Pages—only the static assets such as HTML, CSS, JavaScript, and other resources required for a static site.
Before creating a new orphan Git branch, make sure you have already cloned the repository. For example:
git clone git@github.com:username/repo.git
The examples below assume you are already inside the repository directory:
cd repo
Create a New Git Orphan Branch with the switch Command
Recent versions of Git introduce a new command called switch. When used with the --orphan option, it creates a new branch without a parent named <new-branch>, and removes all tracked files.
From the official documentation:
DESCRIPTION
Switch to a specified branch.
The working tree and the index are updated to match the branch.
All new commits will be added to the tip of this branch.
...
OPTIONS
--orphan <new-branch>
Create a new unborn branch, named <new-branch>. All tracked files are removed.
Here’s an example of how to use it:
git switch --orphan new-branch
Below is a complete example of creating an orphan branch called new-branch, adding a new README.md, and committing it:
$ git switch --orphan new-branch
Switched to a new branch 'new-branch'
$ echo "# README" > README.md
$ git add .
$ git commit -m "Init new-branch"
[new-branch (root-commit) 09392f5] Init new-branch
1 file changed, 1 insertion(+)
create mode 100644 README.md
$ git status
On branch new-branch
nothing to commit, working tree clean
Very easy, right?
Create a New Git Orphan Branch with the checkout Command
If you’re using an older version of Git that doesn’t support the switch command yet, you can still create an orphan branch using git checkout.
Here’s how to create a new orphan branch with checkout:
git checkout --orphan new-branch
Note that when using the checkout command, all tracked files from the previous branch will still be present. This means the new orphan branch is not empty by default—you’ll need to remove those files manually to start fresh.
Here’s an example where a new orphan branch is created, existing files are removed, a new README.md file is added, and then committed:
$ git checkout --orphan new-branch
Switched to a new branch 'new-branch'
$ git reset --hard
$ echo "# README" > README.md
$ git add .
$ git commit -m "Init new-branch"
[new-branch (root-commit) bc7d544] Init new-branch
1 file changed, 1 insertion(+)
create mode 100644 README.md
$ git status
On branch new-branch
nothing to commit, working tree clean
That’s it—you now have a fresh, empty orphan Git branch.
Ready-to-use Shell Scripts
You don’t really need to remember all the commands. You can always wrap them in a Bash function.
Here’s a Bash function to create a new orphan Git branch using the switch command:
function git-switch-branch-orphan() {
if [[ -z "$1" ]]; then
echo "Error: Branch name parameter is required" >&2
return 1
fi
git switch --orphan "$1"
touch README.md
git add .
git commit -m "Created a new branch '$1'"
}
Or download it from GitHub: https://github.com/junian/shell-scripts/blob/master/git/git-switch-branch-orphan.sh
Here’s the Bash function to create a new orphan Git branch using the checkout command:
function git-checkout-branch-orphan() {
if [[ -z "$1" ]]; then
echo "Error: Branch name parameter is required" >&2
return 1
fi
git checkout --orphan "$1"
git reset --hard
touch README.md
git add .
git commit -m "Created a new branch '$1'"
}
Or download it from GitHub: https://github.com/junian/shell-scripts/blob/master/git/git-checkout-branch-orphan.sh
Conclusion
Creating a new orphan Git branch is actually very straightforward. Use the switch command for a cleaner and more modern workflow, and fall back to the checkout command only when working with legacy Git versions.
As usual, if you have any questions or a better method, feel free to leave a comment below. Thanks for reading, and see you next time!
