Skip to content

Creating a Clean Git Branch with Worktree and No History

Published: at 12:00 PM

When working with Git worktrees, I sometimes need a brand-new branch that starts from scratch—completely clean with no commit history.

Here’s the simplest way I’ve found to do it.

Creating a Clean Git Branch with Worktree and No History

🛠️ Step-by-Step Guide

1. Create an orphan branch

From the root of your main repository, create a new orphan branch. This branch will have no previous commit history.

git checkout --orphan new-branch

At this point, all files from your current branch are still present in your working directory, but they are staged as new files since this branch has no history.


2. Unstage everything

Reset the index so nothing is staged:

git reset .

3. Clean the working directory

Now remove all tracked and untracked files so the branch is completely empty.

git clean -fxfd

⚠️ Make sure you are on the orphan branch before running this command. This permanently deletes files in your working directory.


4. Create the first commit

Create an empty initial commit:

git commit --allow-empty -m "new-branch"

You can also add files (like a README.md) before committing and skip --allow-empty if you prefer.


5. Switch back to your main branch

git checkout master

6. Create a worktree for the new branch

Now attach the new branch to its own worktree directory:

git worktree add ../new-branch new-branch

This creates a new folder at ../new-branch with the clean branch checked out.


✅ You’re Done!

You now have a completely clean branch with no history, attached to its own worktree. Your main branch stays untouched, and your new branch lives in its own directory.

This setup is great for version-specific builds, templates, boilerplates, or isolated experiments.


🔁 Automating the Process with a Git Alias

If you find yourself creating clean orphan worktree branches often, you can automate the entire sequence with a Git alias.

Since the only thing that changes is the branch name, the alias can accept it as an argument.


Option 1: Git Alias (Linux / macOS)

On Linux and macOS, single quotes work correctly in the shell. You can run:

git config --global alias.create-orphan-worktree '!f() { git checkout --orphan "$1" && git reset . && git clean -fxfd && git commit --allow-empty -m "$1" && git checkout master && git worktree add "../$1" "$1"; }; f'

Usage

git create-orphan-worktree new-branch

This will:


Option 2: Git Alias (Windows / Git Bash Compatible)

On Windows, single quotes can cause parsing issues. Use double quotes and escape the dollar signs instead:

git config --global alias.create-orphan-worktree "!f() { git checkout --orphan \"$1\" && git reset . && git clean -fxfd && git commit --allow-empty -m \"$1\" && git checkout master && git worktree add \"../$1\" \"$1\"; }; f"

Usage

git create-orphan-worktree new-branch

Why This Works


⚠️ Important Notes


Alternative: Shell Function (Optional)

If you prefer something easier to modify later, you can add a shell function to your ~/.bashrc or ~/.zshrc:

create-orphan-worktree() {
  local branch_name="$1"
  if [ -z "$branch_name" ]; then
    echo "Usage: create-orphan-worktree <branch-name>"
    return 1
  fi

  git checkout --orphan "$branch_name"
  git reset .
  git clean -fxfd
  git commit --allow-empty -m "$branch_name"
  git checkout master
  git worktree add "../$branch_name" "$branch_name"
}

Then reload your shell:

source ~/.bashrc
# or
source ~/.zshrc

And use it like this:

create-orphan-worktree 1.21.4-fabric

This saves you from typing six commands every time and keeps your workflow consistent.


Previous Post
PrettierRC File Generator
Next Post
How to Fix Problems Uploading Code to Arduino Nano