Library / SDK
tschaub/gh-pages avatar
tschaub/gh-pages

gh-pages: publishing replaces the branch unless you ask it to add

General purpose task for publishing files to a gh-pages branch on GitHub

3,348 stars201 forksJavaScriptMIT

At a glance

What is it?
gh-pages is a small JavaScript module and command line pair that copies a build directory into a publishing branch by cloning the repository, committing and pushing. It is easy to use and it is destructive by design: the target branch is treated as a mirror of your source files, and the option that changes that is easy to miss. The version on npm is 6.3.0 while the branch has moved since.
Who is it for?
gh-pages is the right size of tool for a static site built in JavaScript, because it is a library and a command with no service to run and no account to create, and the set of options covers the cases that actually come up, a custom branch, a subdirectory, a custom domain, a tag per deploy. Two things to check before you wire it into a release script.
Can I use it commercially?
Yes. MIT is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
Is it still maintained?
Activity is slowing. The repository last received commits 8 months ago.
What is it written in?
Mainly JavaScript, according to GitHub's language statistics.

Answers come from the project's GitHub data, last synced on November 25, 2026, and from our analysis. They are not legal advice.

From daily snapshots

Star history

  • tschaub/gh-pages3,348 ★ · +3
Star history
Repository2026-09-242026-11-25
tschaub/gh-pages3.3k3.3k

3 Stars in the last 62 days, measured from the daily GitHub snapshot. Compare with other projects

Editorial analysis

Publishing clones the repository, rewrites the branch, and pushes to origin

The whole mechanism fits in one paragraph, and it is worth knowing because it explains the side effects. Calling publish creates a temporary clone of the current repository, creates the publishing branch if one does not already exist, copies over all files from the base directory or only those matching patterns from an optional src setting, commits the changes, and pushes to the origin remote.

There is one ordering detail that matters when the branch already exists. It is first updated with all commits from the remote, and only then are the commits from your source files added on top. So the operation is a fast-forward of history plus a new commit, not a replacement of the branch by force.

js
var ghpages = require('gh-pages');

ghpages.publish('dist', function(err) {});

The two signatures are publish with a directory and a callback, or publish with a directory, an options object and a callback. The first directory argument is the base path for everything the src patterns select, so the relationship between the two is that src filters inside the directory you named.

The command line equivalents exist for the branch and repository options, which are the two you are most likely to need from a shell.

The branch is a mirror by default, so files outside your source set are deleted

This is the behaviour to internalise before the first deploy. The file states it as a note with emphasis: any files in the publishing branch that are not in the src files will be removed. A branch that is your output directory and nothing else is the easiest thing to reason about, and that is the default.

The escape hatch is one option:

js
ghpages.publish('dist', {add: true}, callback);

With add set, the task only adds and never removes existing files. Everything already on the branch survives, including files that no longer exist in your source. That is what you want for a branch you also keep hand-written pages on, and it is also how a stale file becomes permanent, since nothing sweeps it for you afterwards.

What gets selected is governed by src, which defaults to a single pattern matching everything, expressed as minimatch patterns and accepting either a string or an array. Dotfiles are the exception to that: by default a file starting with a dot is ignored unless it is explicitly listed in src, and there is a separate dotfiles option to include the ones that would otherwise match your patterns.

The file says Node > 14 and the manifest says >= 10

The stated requirement and the enforced requirement disagree. The getting started section says the module requires Git 1.9 or later and Node greater than 14. The package manifest declares its engines field as node greater than or equal to 10.

Neither number matches the dependency set the same manifest carries. The runtime dependencies include fs-extra at 11, commander at 14, tinyglobby, filenamify, find-cache-dir and email-addresses, which belong to a much later generation of Node than 10 supports, and the development dependencies include eslint 9 and mocha 11. So the manifest's floor is a leftover from an earlier era of the package rather than a statement about what the code now needs.

That matters in practice for continuous integration, where the manifest is what tools read. A pipeline that trusts the engines field can select a runtime older than the dependencies allow and fail during install rather than during your build.

Two binaries ship, and the option reference stops mid-heading

The manifest maps two commands. One is gh-pages, pointing at the script that performs the publish. The other is gh-pages-clean, a second entry point that the file never describes anywhere in its text. If you are choosing between this package and something else, that second command is the reason to look in the repository rather than the documentation, since a clean command implies behaviour around stale branches that the option list does not cover.

The option reference has the same problem from the other end. It walks through dir, src, branch, dest, dotfiles, nojekyll, cname, add, repo, remote, tag, message and user, and then stops in the middle of the next heading. Every option has a type, a default and an example, except whatever comes after user.

What is documented is still most of the surface. src selects files with minimatch patterns. dest places content in a subdirectory of the target branch rather than at its root. cname writes a CNAME file for a custom domain. nojekyll writes the file that bypasses Jekyll on GitHub Pages. tag creates a tag after committing on the target branch, message sets the commit message with a default of Updates, and remote and repo change which remote and which repository is pushed to.

The commit identity is your problem when git has none

One option exists for a failure that otherwise looks like a bug. If you run the task in a repository without user.name or user.email in the git configuration, or on a machine with no global values for them, git will refuse to let it commit. The user option accepts a name and an email as strings and supplies the committer identity for the commit.

js
ghpages.publish('dist', {
  user: {
    name: 'Joe Code',
    email: '[email protected]'
  }
}, callback);

The other three options that change output rather than destination are equally short. message overrides the commit message for all commits. tag creates a tag after the commit, so a deployment can be pointed at from the repository's tags. And dest moves the published files into a subdirectory of the branch, which is what you want when the branch also serves something at its root:

js
ghpages.publish('dist', {
  dest: 'static/project'
}, callback);

That combination is common enough that it is worth deciding early whether your site lives at the branch root or in a subdirectory, because it changes every URL you link to.

Linting runs before every test, and the lint target includes the binary

The three npm scripts are arranged so that you cannot run the tests without the linter having run first. A pretest script invokes lint, and lint covers four paths: lib, test, bin and the root plugin file. The test script itself is mocha in recursive mode over the test directory.

That is worth knowing for two reasons. A formatting problem stops the test run rather than surfacing as a test failure, which is convenient locally and annoying in a pipeline. And because bin is linted, the two command line entry points are covered by the same rules as the library, so a change to the published scripts is checked before it ships.

The test dependencies describe the shape of the suite: chai and sinon for assertions and stubbing, tmp for throwaway directories, and dir-compare for comparing directory trees, which is exactly what you need when the thing under test copies a directory into a branch. Chai is held at version 4 while the runner is mocha 11, which is the older CommonJS line of that assertion library rather than the newer major.

The lint configuration is a flat config file using a shared config package the author also publishes, so the rules travel with other projects rather than living only here.

npm version 6.3.0 from January 2025, with commits on the branch since March

The version line is worth reading carefully if you depend on this in a build. The published releases are 6.1.1 in December 2023, 6.2.0 in October 2024 and 6.3.0 on 2 January 2025. The default branch was last pushed on 26 March 2026, so there are commits on the branch that no published version contains.

That gap is normal for this kind of small package and it is the reason to pin. If you install from npm you get 6.3.0; if you install from the branch you get whatever is on the tip, and nothing in the file tells you what changed between them.

Two more small details about the repository itself. The homepage field in the manifest points at the package page on npm rather than at a documentation site, so the readme is the only prose documentation, and the readme is named in lowercase along with the changelog, which is why neither shows up in the usual README and CHANGELOG slots on a repository host.

The top-level layout is otherwise conventional: lib for the implementation, bin for the two entry points, tasks, test, and a plugin file at the root that the lint script explicitly covers.

Editorial conclusion

gh-pages is the right size of tool for a static site built in JavaScript, because it is a library and a command with no service to run and no account to create, and the set of options covers the cases that actually come up, a custom branch, a subdirectory, a custom domain, a tag per deploy. Two things to check before you wire it into a release script. The default behaviour is to make the branch match your files, so anything hand-placed in that branch is removed on the next publish unless you pass the add option, and that is the difference between a deploy and an upload. And the stated Node requirement is inconsistent between the file and the manifest, so check which one your runtime satisfies rather than trusting either. The npm version is 6.3.0 from January 2025, and the branch has been pushed since, so pin the version your build depends on.

Frequently asked questions

How do I publish a folder to a gh-pages branch?

Install it with npm install gh-pages --save-dev, then call ghpages.publish('dist', callback). That creates a temporary clone of the current repository, creates the branch if it does not exist, copies the files, commits the changes and pushes to the origin remote.

Does gh-pages delete existing files on the target branch?

Yes, by default. Any files in the branch that are not in your src files will be removed, because the branch is treated as a mirror of the source. Set the add option to true to add new files and leave existing ones untouched.

Which files does gh-pages publish by default?

Everything matched by the src patterns, which default to '**/*' as minimatch patterns. Files starting with a dot are ignored unless they are explicitly listed in src, or unless the dotfiles option is set to true.

What are the runtime requirements for gh-pages?

The file states Git 1.9 or later and Node greater than 14, while the package manifest declares node greater than or equal to 10 in its engines field.

Which command line commands does the gh-pages package install?

Two: gh-pages, mapped to bin/gh-pages.js, and gh-pages-clean, mapped to bin/gh-pages-clean.js. The branch option has the short form -b and the repository option has -r.

Official sources

  1. License: MIT
  2. Project website
  3. README
  4. Releases
  5. tschaub/gh-pages on GitHub
Add this badge to your README

If you maintain this project, the badge below links readers to this analysis and shows its maintenance status from the daily GitHub snapshot. Paste the markdown into your README; add ?metric=license or ?metric=stars to the image URL for a different field.

Add this badge to your README

markdown
[![Hysen Labs](https://hysenlabs.com/badge/tschaub-gh-pages.svg)](https://hysenlabs.com/projects/tschaub-gh-pages)

Star history chart for your README

markdown
[![Star history](https://hysenlabs.com/chart/tschaub-gh-pages.svg)](https://hysenlabs.com/projects/tschaub-gh-pages)

Was this useful?