Skip to content

Creating a GitHub Repository from Existing Source Files

This guide shows you how to set up GitHub deployments on WordPress.com by starting with local source files. It assumes you have a GitHub account and set up Git on your computer.

Create a local Git repository

If you already have a local Git repository, you can skip this step and jump to Adding your files to GitHub.

I have a wp-env project

This is the official WordPress development environment based on Docker. This section assumes you have already set up a wp-env project. If you haven’t, you can follow this guide

Let’s assume you have created a new theme and plugin. Your project file structure will look something like this:

<project root>/wp-content/themes/mytheme
<project root>/wp-content/plugins/myplugin

or

<project root>/themes/mytheme
<project root>/plugins/myplugin
  1. At the <project root> in your terminal, run git init to create your Git repository.
  2. Next, run git add . to tell Git to start tracking your files.
  3. Then, run git commit -m “Initial commit” to commit your files.

You can now jump to Adding your files to GitHub.

I have a Studio by WordPress.com project

Let’s assume you have created a new theme and plugin in your Studio project. Your project file structure will look something like this:

site-folder/...
site-folder/wp-content/...
site-folder/wp-content/themes/mytheme
site-folder/wp-content/plugins/myplugin
  1. In Studio, select your local site and click Open in Terminal. The current working directory will be your site folder. 
  2. Run cd wp-content.
  3. Run git init to create your Git repository.
  4. Next, run git add themes/mytheme plugins/myplugin to tell Git to track your files.
  5. Finally, run git commit -m "Initial commit".

I have a Local by Flywheel project

This guide shows you how to take a Local project and create a repository from the files you added. Sites created with Local contain a full WordPress installation. While you can add all of these to GitHub, it is recommended that you create the repository from only the files you added. 

Let’s assume you have created a new theme and plugin in your Local project. Your project file structure will look something like this:

public/...
public/wp-content/...
public/wp-content/themes/mytheme
public/wp-content/plugins/myplugin
  1. In Local, click Open site shell. The current working directory will be public, which is the site’s root folder.
  2. Run cd wp-content.
  3. Run git init to create your Git repository.
  4. Next, run git add themes/mytheme plugins/myplugin to tell Git to track your files.
  5. Finally, run git commit -m “Initial commit”

You can now jump to Adding your files to GitHub.

I have a wp-now project

This guide shows you how to take a wp-now project and create a local Git repository from it.

Let’s assume you created a plugin with wp-now and your files are in the current working directory:

index.php
style.css
  1. Run git init to create your Git repository.
  2. Next, run git add . to tell Git to track your files.
  3. Finally, run git commit -m “Initial commit”

You can now jump to Adding your files to GitHub.

I have files on my WordPress.com site

In this guide, we will use the rsync command. You can also copy your files to your local file system using an FTP client.

Let’s assume you have a custom theme on your WordPress.com site and wish to create a deployment for it. Your theme will be at /htdocs/wp-content/themes/mytheme.

  1. Go to wordpress.com/hosting-config/:your-site and enable SSH if not already enabled.
The SSH access setting on WordPress.com with the SSH address and the toggle enabled
  1. Copy the SSH address, e.g. instantdiskette5.wordpress.com@sftp.wp.com.
  2. Identify the path to the content you want to download:
    • Each theme lives in its own folder under /htdocs/wp-content/themes/.
    • Each plugin lives in its own folder under /htdocs/wp-content/plugins/.
  3. Create a local folder by running mkdir [PROJECT_FOLDER] && cd [PROJECT_NAME], where [PROJECT_NAME] is the name of your project folder.
  4. Download the theme or plugin from WordPress.com into your local project folder by running rsync -avzh [SFTP_USERNAME]@sftp.wp.com:[PATH_TO_CONTENT_FOLDER] ..
    • [SFTP_USERNAME] is your SFTP credential’s username.
    • [PATH_TO_CONTENT_FOLDER] is the absolute path to the content (theme or plugin) you want to download.
      • Let’s assume you have a custom theme “mytheme” on your WordPress.com site and wish to create a deployment for it. Your theme should be at /htdocs/wp-content/themes/mytheme/, and that should be the path used in the command.

Note: It’s important to include the trailing / after the theme name to ensure content inside the folder gets downloaded.

  1. You will then be prompted to enter your password, which can be found under SFTP credentials of your Server Settings at wordpress.com/hosting-config/:your-site.
  2. Wait for the download to complete, and then change directory into your content folder by running cd [CONTENT_FOLDER_NAME].
  3. Then, run git init to create a Git repository.
  4. Next, run git add . to tell Git to track your files.
  5. Finally, run git commit -m “Initial commit”

Common Error

A common error encountered when running the rsync command is:

receiving file list ... unexpected tag 103 [receiver]rsync error: error in rsync protocol data stream (code 12)

This often happens when the wrong path is supplied to the rsync command.

Adding your files to GitHub

You should have a local Git repository before starting this step. We will create a repository on GitHub and push (upload) the contents of the local repository there.

  1. Navigate to https://github.com/new and
    • Enter a Repository name.
    • Choose Public or Private. If private, you will need to generate an access token before pushing your local repository to GitHub.
    • Important: Leave all other options as default. You can add a README and license later.
    • Click on the Create Repository button.
The 'Create a new repository' screen on GitHub with fields for Repository template, owner, repository name, and description.
  1. Take note of your GitHub repository URL, which will be https://github.com/<account name>/<repository name>.
  2. Back at the root of your project, run git remote add origin https://github.com/<account name>/<repository name>.git, which will link your local repository to your GitHub repository. Locally, your remote GitHub repository will be known as origin.
  3. Next, run git remote -v to verify it worked.
    • You should see something like:
origin https://github.com/<account name>/<repository name> (fetch)
origin https://github.com/<account name>/<repository name> (push)
  1. Now run git status and note the branch name you’re using.
  2. Run git push -u origin BRANCH with the branch name from the previous step.
  3. If your repository is private, create a new access token here.
    • Enter a value for Token name, select the repositories the token is valid for and then go to the Contents repository permission and select Read and write.
The Contents setting under 'personal access tokens' on GitHub with 'Access: read and write' selected
  1. Copy the access token from GitHub into the command line when prompted.
  2. Check GitHub and confirm your local files are now in your repository.

You can now jump to Setting up a WordPress.com deployment.

Links:

Setting up a WordPress.com deployment

Once you have your files in GitHub repository, you can continue with our GitHub Deployments guide.

Last updated: July 01, 2024