Skip to content

Instantly share code, notes, and snippets.

@nobuoka
Last active November 22, 2019 15:38
Show Gist options
  • Save nobuoka/d0f088df57d50e4cda1a to your computer and use it in GitHub Desktop.
Save nobuoka/d0f088df57d50e4cda1a to your computer and use it in GitHub Desktop.
The way to commit built files in subdirectory to gh-pages branch from an arbitrary branch

Committing a subfolder to the gh-pages branch from the other branch

Sometimes you want to commit a subfolder on an arbitrary branch (rather than gh-pages branch) as the root directory to the gh-pages branch. You will want to do so when, for example, the files to be published on GitHub Pages are generated by a build system.

This document shows the way to commit a build/gh-pages directory to the gh-pages branch by using Git plumbing commands. In the following example, Windows PowerShell is used as a shell environment.

Step 1 : Create a tree object

git add -f ./build/gh-pages
$treeObjId = git write-tree --prefix=build/gh-pages
git reset -- ./build/gh-pages

At first, create the Git tree object by using git write-tree command. To create a tree object that represents a subdirectory, use --prefix option.

Step 2 : Create a commit with the created tree object

$commitId = git commit-tree -p gh-pages -m "YOUR COMMIT MESSAGE" $treeObjId

Next, you need to create a new commit by using git commit-tree command. Its parent is a commit which is referred by gh-pages branch, and its tree object is the created one at step 1.

Step 3 : Update the gh-pages branch to refer to the created commit

git update-ref refs/heads/gh-pages $commitId

Last, make gh-pages branch refer to the created commit at step 2 by using git update-ref command.

Actual example

Related page

  • Deploying a subfolder to GitHub Pages : This shows the way to push the committed subdirectory on the master branch to remote gh-pages branch by using git subtree push command.
    • You need to commit the built files on the master branch (or arbitrary branch other than the gh-pages branch) to deploy GitHub Pages.
@runeflobakk
Copy link

Thank you! This worked great! 💯
If one needs to (like I did), the PowerShell syntax for variable assignments can be adapted to Bash and similar like this:

treeObjId = $(git write-tree --prefix=build/gh-pages)

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