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 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.
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
- Go to
wordpress.com/hosting-config/:your-site
and enable SSH if not already enabled.
- Copy the SSH address, e.g.
[email protected]
. - Create a local folder by running
mkdir myproject && cd myproject
. - Now copy your remote theme into your local project folder by running
rsync -avzh SITE [email protected]:/htdocs/wp-content/themes/mytheme .
. Make sure to change SITE NAME with your site name. - You will need to enter your password, which you can get at
wordpress.com/hosting-config/:your-site
. - Wait for the copy to complete, and then change directory into your theme folder by running
cd mytheme
. - 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”
.
You can now jump to Adding your files to GitHub.
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: June 04, 2024