Skip to content

Instantly share code, notes, and snippets.

@seanbuscay
Created June 27, 2013 15:26
Star You must be signed in to star a gist
Embed
What would you like to do?
Create an orphan branch in a repo.
cd repository
git checkout --orphan orphan_name
git rm -rf .
rm '.gitignore'
echo "#Title of Readme" > README.md
git add README.md
git commit -a -m "Initial Commit"
git push origin orphan_name
@KartikSoneji
Copy link

Note that using git switch --orphan <branch name> automatically removes ALL files from the working tree.

@kurahaupo
Copy link

@anfxanfx
Copy link

Commenting on KartikSoneji (Jul 15) above: git: 'switch' is not a git command. See 'git --help'.

@KartikSoneji
Copy link

@anfxanfx
git switch was introduced in git 2.23, you might need to update your git client.
https://git-scm.com/docs/git-switch

@kurahaupo
Copy link

For those who don't have access to a bleeding-edge version of git, I have a simple work-around: make an empty commit when the repo is new, tag it, and then use that as the base for any orphan branches.

git init
git commit --allow-empty
git tag empty

then git checkout --orphan new_branch empty.

As a bonus, you can now use git rebase -i empty, to amend or remove what would otherwise have been the "initial" commit.

@KartikSoneji the documentation still (as of 2.29.0) says:

THIS COMMAND IS EXPERIMENTAL. THE BEHAVIOR MAY CHANGE.

@mlandes
Copy link

mlandes commented Feb 9, 2022

On git 2.23, all I needed was:

git switch --orphan <branch_name>
git commit --allow-empty -m "<commit_msg>"
git push origin <branch_name>

@SeanHorner
Copy link

shadiakiki1986

you're looking for "checkedout", not "checkout'ed" 👍

@kurahaupo
Copy link

@SeanHorner see gist:7df6c8a5…

(I note that checkout is a git verb, so in this context it's not so unreasonable to conjugate it as checkouts and checkouted - just as long as nobody says those out loud.)

@SeanHorner
Copy link

@SeanHorner see gist:7df6c8a5…

(I note that checkout is a git verb, so in this context it's not so unreasonable to conjugate it as checkouts and checkouted - just as long as nobody says those out loud.)

Ah, didn't think about it in that context, was using my linguist brain, not coding brain xD. Very interesting (and thorough) discussion in the linked gist 👍

@MrSrv7
Copy link

MrSrv7 commented Sep 6, 2022

Note that using git switch --orphan <branch name> automatically removes ALL files from the working tree.

Note that whatever is ignored by .gitignore won't be removed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment