Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save codesharpdev/133e205b2133fca792e56eecf63a4827 to your computer and use it in GitHub Desktop.
Save codesharpdev/133e205b2133fca792e56eecf63a4827 to your computer and use it in GitHub Desktop.

Revisions

  1. @ErickPetru ErickPetru created this gist Sep 13, 2019.
    44 changes: 44 additions & 0 deletions worktree-publish-to-gh-pages.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    # Setup

    First of all, you need to have a `gh-pages`. If you don't have, create:

    ``` bash
    git branch gh-pages
    ```

    This makes a branch based on the `master` HEAD.
    It would be okay but the files and the git history of `master` branch are not meaningful on `gh-pages` branch.
    Using an `--orphan` branch, you can initialize `gh-pages` in a clean way.

    ``` bash
    git checkout --orphan gh-pages
    git reset --hard
    git commit --allow-empty -m "Init gh-pages branch"
    git checkout master
    ```

    Then, mount the branch as a subdirectory using `git worktree`:

    ``` bash
    git worktree add dist gh-pages
    ```

    If you didn't ignore the `dist` folder, ignore it so that you don't add generated files accidentally in your `master` branch commits.

    ``` bash
    echo "dist/" >> .gitignore
    ```

    # Deploy

    Every time you build the static bundle, generated files are in `dist` directory.
    Since `dist` folder is now `gh-pages` branch, you can deploy it directly by just creating a commit and pushing it.

    ``` bash
    cd dist
    git add --all
    git commit -m "Deploy on gh-pages updated"
    git push origin gh-pages
    ```

    This way nothing was added to the `master` branch history, keeping it clean.