brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: git switch with orphan starts a branch with no history

Git
~/tips/tip-git-switch-orphan-empty-branch

git switch --orphan gh-pages

Creates an empty branch that shares no commits with the rest of the repository.

Some branches have nothing to do with the code, like a gh-pages branch with a built website or a branch for documentation. They don’t need the project’s history at all.

git switch --orphan gh-pages

Git creates the branch with no parent commit and empties your working tree of tracked files. The first commit you make on it becomes a new root, completely separate from main. Add the files you want, commit, and push like any branch:

echo "Hello" > index.html
git add index.html
git commit -m "First page"
git push -u origin gh-pages

I like that the branch stays small. A website build doesn’t need years of source history, and cloning only that branch with --single-branch is quick.

Don’t panic when your files disappear after the switch. They’re still safe on the branch you came from, and git switch main brings them back. Untracked and ignored files stay where they are, so check git status before you run git add . on the new branch, or you’ll commit your build folder or node_modules by accident.

Because the branch shares no history, merging it into main later gives an error about unrelated histories. That’s intended, and it’s a sign you probably didn’t want to merge it anyway.

Further reading: git-switch documentation

Going deeper: Moving on from hdtv: how to export your TV show list to Trakt.tv

More Git tips git show-branch main feature git log --stat -5 git merge --no-ff feature -m "Merge feature" git merge --squash feature git merge --abort All Git tips (144) All topics

Link