日本語

How to Create an Orphan Branch with No History Using git switch –orphan

​​Git​command line

Running git switch --orphan <branch-name> switches you to an empty branch that is not connected to any existing commits. Use it when you want to manage content in the same repository that is separate from the main source code, such as a branch that holds only a publishing page or documentation. I checked step by step how the output of git status and git log changes before and after the operation in a test environment.

Note

The test environment is Ubuntu 24.04 with Git 2.43.0. I set the author name and email to example values (Neko <neko@example.com>). For the remote, I used an empty repository created in the same environment. git switch was introduced in Git 2.23.0, so older versions of Git do not have it. I have not tested on Windows or macOS, or pushing to hosting services such as GitHub.

The repository before the operation

I tested in a repository with two commits on the main branch. memo.txt is a file that Git is not tracking yet.

console
$ git branch
* main

$ git log --oneline
ea9e292 送料を400円に変更
ae84adb 最初のコミット

$ ls
README.md
memo.txt
price.txt

$ git status --short
?? memo.txt

If there are modified files, the switch stops without changing branches

On main before creating site, I modified the tracked file price.txt and ran git switch --orphan site. It stopped without switching. The error says the changes would be overwritten, and asks you to commit or stash them first.

console
$ echo "送料: 0円" > price.txt && git status --short
 M price.txt
?? memo.txt

$ git switch --orphan site
error: Your local changes to the following files would be overwritten by checkout:
	price.txt
Please commit your changes or stash them before you switch branches.
Aborting

$ git branch --show-current
main

I ran git restore price.txt to discard the change before moving on to the next step.

Switching with git switch –orphan

I created a branch named site with no history. After switching, README.md and price.txt disappeared from the working directory, and only memo.txt remained.

console
$ git switch --orphan site
Switched to a new branch 'site'

$ ls -A
.git
memo.txt

The --orphan section of the official git switch reference says that all files tracked by Git are removed. Because memo.txt is not tracked by Git, it is not affected and remains. The removed README.md and price.txt are in the commits of main, so they come back when you return to main (I confirm this in the next section).

git status shows "No commits yet". git log fails.

console
$ git status
On branch site

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	memo.txt

nothing added to commit but untracked files present (use "git add" to track)

$ git log
fatal: your current branch 'site' does not have any commits yet

site does not appear in the output of git branch yet. git branch --show-current, which prints the name of the current branch, shows site.

console
$ git branch
  main

$ git branch --show-current
site

The git switch reference calls a branch that has a name but no commits yet an "unborn branch".

Going back to the original branch before the first commit removes the branch

If you switch back to main without making a commit, the site branch disappears. Trying git switch site fails because the branch is not found.

console
$ git switch main
Switched to branch 'main'

$ git branch
* main

$ ls
README.md
memo.txt
price.txt

$ git switch site
fatal: invalid reference: site

When you return to main, README.md and price.txt come back as well. To create it again, run git switch --orphan site once more.

Making the first commit creates a commit with no parent

I switched to site again, created the publishing page index.html, and committed it. The commit output shows (root-commit).

console
$ git switch --orphan site
Switched to a new branch 'site'

$ printf "<h1>猫の手ストア</h1>\n" > index.html

$ git add index.html && git commit -m "公開用のページを追加"
[site (root-commit) f2a4551] 公開用のページを追加
 1 file changed, 1 insertion(+)
 create mode 100644 index.html

Only now does site appear in the output of git branch. The history visible with git log is just the one commit I just made.

console
$ git branch
  main
* site

$ git log --oneline
f2a4551 公開用のページを追加

I displayed the commits of all branches together with their parent commits (%p is the parent hash). f2a4551 has no parent, just like the first commit ae84adb on main. git rev-list --max-parents=0, which lists only commits with no parents, also shows these two.

console
$ git log --all --format="%h 親:%p %s"
f2a4551 親: 公開用のページを追加
ea9e292 親:ae84adb 送料を400円に変更
ae84adb 親: 最初のコミット

$ git rev-list --max-parents=0 --abbrev-commit --all
f2a4551
ae84adb
Diagram showing that ae84adb and ea9e292 on the main branch are connected as parent and child, while f2a4551 on the site branch has no parent commit, so there is no connection between the two histories
The main history and the site history created with git switch –orphan

git merge-base, which finds the common ancestor of two branches, printed nothing and exited with code 1. main and site have no common ancestor commit.

console
$ git merge-base main site; echo "終了コード: $?"
終了コード: 1

Even after the commit, memo.txt, which Git does not track, is still in the working directory.

console
$ git status --short
?? memo.txt
Warning

If you run git add . after git switch --orphan to add everything at once, untracked files such as memo.txt also go into the first commit of the new branch. When I tried this with another empty branch, git status --short after git add . showed A memo.txt. Name the files you want to add explicitly, and check the contents with git status --short before committing.

When a branch with the same name already exists

While on site, passing the name of an existing branch, main, gives an error.

console
$ git switch --orphan main
fatal: a branch named 'main' already exists

Returning to main restores the original files and history

After committing on site, I switched back to main. README.md and price.txt came back, and index.html is no longer visible. The history on main is the same as before the operation.

console
$ git switch main
Switched to branch 'main'

$ ls
README.md
memo.txt
price.txt

$ git log --oneline
ea9e292 送料を400円に変更
ae84adb 最初のコミット

When I tried to git merge site into main, which has no common ancestor, it stopped with the error "refusing to merge unrelated histories". I cover how to fix this error in a separate article, "How to Fix the Git unrelated histories Error".

console
$ git merge site
fatal: refusing to merge unrelated histories

git checkout –orphan keeps the files and switches

git checkout --orphan, which existed before git switch was added, can also create a branch with no history. However, the state after switching is different. Running git checkout --orphan docs from main kept README.md and price.txt and staged them (A) as files to include in the next commit.

console
$ git checkout --orphan docs
Switched to a new branch 'docs'

$ git status --short
A  README.md
A  price.txt
?? memo.txt

$ ls
README.md
memo.txt
price.txt

According to the --orphan section of the official git checkout reference, this behavior suits cases where you want to publish the current contents without showing the history. The same section says that running git commit -a now creates a first commit containing the same files. If you want completely different contents, it suggests running git rm -rf . at the top of the working directory to empty it. When I ran it, only memo.txt, which Git does not track, remained, just as right after git switch --orphan.

console
$ git rm -rf -q . && git status --short && ls
?? memo.txt
memo.txt

I went back to main without committing. Because I never committed on docs, it does not remain in the output of git branch.

console
$ git switch main
Switched to branch 'main'

$ git branch
* main
  site

If you want to start from an empty branch, git switch --orphan saves you the git rm -rf . step.

Pushing to a remote

The site branch I created can be pushed to a remote with git push, just like any other branch. I used an empty repository created in the same environment as origin and pushed main and site together. (The "To" line that shows the path of the destination folder is omitted from the output.)

console
$ git push -u origin main site
 * [new branch]      main -> main
 * [new branch]      site -> site
branch 'main' set up to track 'origin/main'.
branch 'site' set up to track 'origin/site'.

$ git branch -vv
* main ea9e292 [origin/main] 送料を400円に変更
  site f2a4551 [origin/site] 公開用のページを追加

​Posted by 86