How to connect WP Engine to Git?

We all know Git is a very powerful tool which allows developers to push code from local system to their server via WP Engine. So let’s see how we can connect our WP Engine server with Git and push/pull code to WP Engine from the local machine.

1) Generate SSH key from the terminal
  • First of all, enable Git on your WP Engine site by adding your SSH key to the User Portal. The SSH key has a username and password and opts instead for public and private key pair for authentication.
  • Follow the below steps to create your public/private key pair.
  • For MAC, Use the Spotlight utility and type “Terminal”
  • For Windows, use Git Bash
  • Use “ssh-keygen” to generate a new key is given below. Use your email address in this command.
    ssh-keygen -t rsa -b 4096 -C “your_email@email.com” -f ~/.ssh/wpegitkey
  • Leave the passphrase blank by hitting the enter key, without typing anything. If you want to set a password, you can for security purposes. It cannot be recovered if lost.
  • Press enter or return to confirm the password entry.
  • It will generate your public/private key pair.
  • Make sure the generated file is available in your ~/.ssh/ folder and give a proper name. e.g. wpegitkey.pub.
2) Create SSH Config
  • The SSH Config file decide your system which key file to use when connecting to a specific server hostname.
  • From your local machine, navigate to the .ssh directory
  • For macOS, open terminal and type cd ~/.ssh/
  • For windows, use git bash and navigate to c/users/yourname/.ssh/
  • Run the ‘touch config’ command to create a config file
  • Run the ‘nano config’ command to open the config file.
  • After opening the file, use arrow keys to move at the very bottom of the file and add the below content.
    Host git.wpengine.com
    User git
    Hostname git.wpengine.com
    PreferredAuthenticationspublickey
    IdentityFile ~/.ssh/wpegitkey
    IdentitiesOnly yes

    Save this file and exit.

3) Test SSH Config
  • Run the ‘ssh -v git@git.wpengine.com info’ command to verify your configuration. At bottom of the result, you can see your environment name with production and staging status. E.g.
    R W production/environment
    R W staging/environment
    If the connection was unsuccessful, then please read the output carefully and fix issues.
4) ADD SSH key to the user portal
  • Now we will add the SSH key to the user portal.
  • Go to the user portal and click the environment name to which you would like to connect git.
  • Then click Git Push from the left-hand side menu, create developer name and add SSH key (the content of your public key) and then click on Add Developer.

5) If the WordPress site is setup at your local system then you can move step 8 otherwise download from WP Engine.
  • Go to the user portal.
  • Select the environment name you are connecting Git too.
  • Click on the backup point from the left menu and download the latest zip folder.
  • You will get a zip file path to an email address from which you can download.
  • Move the zip folder in your local system and unzip it.
6) Add standard .gitignore file
7) Confirm git access by ‘ssh git@git.wpengine.com info’ command.
  • If everything properly setup then you will get the developer directory from WP Engine. For the first time, you will be promoted to verify host authentication.
8) From the command line, go to your local WordPress direct path and run the ‘git init’ command.
  • To push your new and modified file, run the ‘gitadd.’ command.
  • After adding files, run commit command as ‘git commit –m “first commit”
9) Set up Remote
  • You need to set remote by running the below command.
  • Git remote add development git@git.wpengine.com:production/devdirectory.git. Replace devdirectory.git with the unique user portal name of your selected environment.
  • Put your dev or staging or development environment name.
  • For staging, use ‘staging ‘and production, use ‘production’ in place of development.
  • Run the ‘git remote –v’ command to verify everything is properly set or not.
  • If you will not get any error then run the ‘git push remote-namebranch-name’ command to push code to the selected environment from your local branch. (e.g. git push development master )
10) To geta pull, run the ‘git pull remote-namebranch-name’ command. (e.g. git pull development master)