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
- At the
<project root>
in your terminal, rungit init
to create your Git repository. - Next, run
git add .
to tell Git to start tracking your files. - 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
- In Studio, select your local site and click Open in Terminal. The current working directory will be your site folder.
- Run
cd wp-content
. - Run
git init
to create your Git repository. - Next, run
git add themes/mytheme plugins/myplugin
to tell Git to track your files. - 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
- In Local, click Open site shell. The current working directory will be
public
, which is the site’s root folder. - Run
cd wp-content
. - Run
git init
to create your Git repository. - Next, run
git add themes/mytheme plugins/myplugin
to tell Git to track your files. - 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
- Run
git init
to create your Git repository. - Next, run
git add .
to tell Git to track your files. - 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.
- Go to
wordpress.com/hosting-config/:your-site
and enable SSH if not already enabled.
- Copy the SSH address, e.g.
instantdiskette5.wordpress.com@sftp.wp.com
. - 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/
.
- Each theme lives in its own folder under
- Create a local folder by running
mkdir [PROJECT_NAME] && cd [PROJECT_NAME]
, where[PROJECT_NAME]
is the name of your project folder. - 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.
- 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
Note: It’s important to include the trailing /
after the theme name to ensure content inside the folder gets downloaded.
- 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
. - Wait for the download to complete, and then change directory into your content folder by running
cd [CONTENT_FOLDER_NAME]
. - Then, run
git init
to create a Git repository. - Next, run
git add .
to tell Git to track your files. - 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.
- 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.
- Take note of your GitHub repository URL, which will be
https://github.com/<account name>/<repository name>
. - Back at the root of your project, run
git remote
, which will link your local repository to your GitHub repository. Locally, your remote GitHub repository will be known asadd
origin https://github.com/<account name>/<repository name>.git
origin
. - 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)
- Now run
git status
and note the branch name you’re using. - Run
git push -u origin BRANCH
with the branch name from the previous step. - 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.
- Copy the access token from GitHub into the command line when prompted.
- 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: August 08, 2024