Skip to content

Instantly share code, notes, and snippets.

@vaidd4
Last active March 29, 2023 23:19
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vaidd4/fbb69e126da0d6f20e1d76dc5be13f92 to your computer and use it in GitHub Desktop.
Save vaidd4/fbb69e126da0d6f20e1d76dc5be13f92 to your computer and use it in GitHub Desktop.

Git orphan branches & working trees

Creating an ophan branch

This command will create a branch with no parent (no commit history) on a repo.

git checkout --orphan <new-branch>

Managing working trees

Manage folders linked to others branches of your repo. Do not confuse them with git submodules which are links to different repositories.

You can create a folder in wich a branch of your repository is checked out.

git worktree <command>

Usages

Exemple for Github Pages with a specific branch providing the static files:

  • Create a new orphan branch: git checkout --orphan gh-pages
  • Clean all (untracked) files: git reset --hard or git rm -rf .
  • Create first commit: git commit --allow-empty -m "Initializing gh-pages branch"
  • Push the new branch on your repo: git push origin gh-pages
  • Get back to your main branch: git checkout master
  • Create the worktree in a public folder: git worktree add -B gh-pages public origin/gh-pages

Now, all the files put in the public folder can simply be committed and pushed to the gh-pages branch.
Example: cd public && git add --all && git commit -m "Published to gh-pages" && git push origin gh-pages.
The rest of your repo will stay in sync with your main branch.

To be sure to not mess with your main branch you can add the folder containing the worktree in your .gitignore file.

@light0x00
Copy link

nice 👍

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