WordPress / 7 MIN READ
WP Git Repo MAMP
How to use Git with MAMP for local WordPress development
From the original Fervor library. Examples may use older package versions.
Comprehensive Tutorial: Using Git with MAMP for Local WordPress Development
Welcome to this step-by-step tutorial on setting up a local WordPress development environment using MAMP and Git. This guide will help you streamline your workflow, maintain version control, and efficiently manage your WordPress projects. Whether you’re a beginner or looking to refine your development process, this tutorial has you covered.
Table of Contents
- Comprehensive Tutorial: Using Git with MAMP for Local WordPress Development
Prerequisites
Before we begin, ensure you have the following installed on your machine:
- MAMP: A free, local server environment.
- Visual Studio Code (VSCode): A powerful code editor.
- Git: Version control system.
- GitHub Account: For remote repository hosting (optional but recommended).
Installing MAMP
-
Download MAMP:
- Visit the MAMP official website and download the appropriate version for your operating system.
-
Install MAMP:
- Follow the installation instructions specific to your OS.
-
Launch MAMP:
- Open the MAMP application.
- Start the servers by clicking on Start Servers. Ensure both Apache and MySQL are running.
-
Verify Installation:
- Open your browser and navigate to
http://localhost:8888/. You should see the MAMP start page.
- Open your browser and navigate to
Setting Up a Local WordPress Site
-
Download WordPress:
- Go to the WordPress.org download page and download the latest version.
-
Extract WordPress:
- Unzip the downloaded file.
- Rename the extracted folder to your desired project name, e.g.,
my-wp-site.
-
Move to MAMP’s
htdocs:- Move the
my-wp-sitefolder to MAMP’shtdocsdirectory:- Mac:
/Applications/MAMP/htdocs/ - Windows:
C:\MAMP\htdocs\
- Mac:
- Move the
-
Create a Database:
- Open your browser and navigate to
http://localhost:8888/phpMyAdmin/. - Click on Databases.
- Enter a database name (e.g.,
my_wp_db) and click Create.
- Open your browser and navigate to
-
Configure WordPress:
- Navigate to
http://localhost:8888/my-wp-site/in your browser. - Follow the WordPress installation wizard:
- Database Name:
my_wp_db - Username:
root - Password:
root(default for MAMP) - Database Host:
localhost - Table Prefix:
wp_(default)
- Database Name:
- Navigate to
-
Complete Installation:
- Provide site details (site title, admin username, password, email).
- Finish the installation and log in to your new local WordPress site.
Exporting and Importing the WordPress Database
If you have an existing WordPress site you’d like to work on locally, follow these steps to import it into your local environment.
Exporting from the Live Site
-
Access phpMyAdmin on Live Server:
- Log in to your hosting account and navigate to phpMyAdmin.
-
Select Database:
- Choose the database associated with your WordPress site.
-
Export Database:
- Click on the Export tab.
- Select the Quick export method and SQL format.
- Click Go to download the database file.
Importing to Local Environment
-
Open phpMyAdmin Locally:
- Navigate to
http://localhost:8888/phpMyAdmin/.
- Navigate to
-
Create a New Database:
- Click on Databases.
- Enter a name (e.g.,
local_wp_db) and click Create.
-
Import the Database:
- Select the newly created database.
- Click on the Import tab.
- Click Choose File and select the exported
.sqlfile. - Click Go to import.
-
Update
wp-config.php:- In your
my-wp-sitefolder, locatewp-config.php. - Update the database details:
define('DB_NAME', 'local_wp_db'); define('DB_USER', 'root'); define('DB_PASSWORD', 'root'); define('DB_HOST', 'localhost');
- In your
-
Update Site URLs:
- In phpMyAdmin, navigate to the
wp_optionstable. - Update the
siteurlandhomefields tohttp://localhost:8888/my-wp-site.
- In phpMyAdmin, navigate to the
Configuring Your Local Environment
-
Hosts File (Optional for Custom Domains):
- If you prefer using a custom local domain (e.g.,
mywpsite.local), modify your hosts file. - Mac/Linux:
- Open Terminal and run
sudo nano /etc/hosts.
- Open Terminal and run
- Windows:
- Edit
C:\Windows\System32\drivers\etc\hostswith administrative privileges.
- Edit
- Add the following line:
127.0.0.1 mywpsite.local - Save and exit.
- Update MAMP’s Apache settings to recognize the new domain if necessary.
- If you prefer using a custom local domain (e.g.,
-
Permalinks:
- Log in to your local WordPress admin.
- Navigate to Settings > Permalinks.
- Choose your preferred permalink structure and save changes to ensure URLs work correctly.
-
Enable Pretty Permalinks:
- Ensure that MAMP’s Apache configuration allows
.htaccessoverrides. - In MAMP, go to Preferences > Apache and set Document Root if needed.
- Ensure that MAMP’s Apache configuration allows
Initializing Git in VSCode
-
Open Project in VSCode:
- Launch VSCode.
- Click on File > Open Folder and select your
my-wp-sitefolder.
-
Initialize Git Repository:
- Open the integrated terminal in VSCode (
Ctrl + ~orView > Terminal). - Run the following command to initialize Git:
git init - This creates a
.gitdirectory in your project folder.
- Open the integrated terminal in VSCode (
-
Configure Git (If Not Already Done):
- Set your username and email:
git config --global user.name "Your Name" git config --global user.email "youremail@example.com"
- Set your username and email:
Setting Up .gitignore
To ensure sensitive files and unnecessary data aren’t tracked by Git, set up a .gitignore file.
-
Create
.gitignore:- In the root of your project (
my-wp-site), create a file named.gitignore.
- In the root of your project (
-
Add the Following to
.gitignore:# Ignore OS files .DS_Store Thumbs.db # Ignore WordPress core /wp-admin/ /wp-includes/ # Ignore content uploads /wp-content/uploads/ # Ignore configuration files wp-config.php # Ignore node modules (if using npm) /node_modules/ # Ignore logs *.log # Ignore environment variables .env # Ignore composer dependencies /vendor/Note: Adjust the
.gitignorerules based on your project’s specific needs. For example, if you’re developing themes or plugins, ensure those directories are included. -
Save the
.gitignoreFile.
Committing and Pushing Changes
-
Stage Changes:
- In the VSCode Source Control panel (usually accessed by clicking the branch icon on the sidebar), you’ll see the list of untracked files.
- Click the + icon next to the files you want to stage or click + next to Changes to stage all.
-
Commit Changes:
- Enter a commit message in the message box, e.g., “Initial commit - Set up local WordPress site.”
- Click the checkmark icon or press
Ctrl + Enterto commit.
-
Connect to a Remote Repository (Optional but Recommended):
- Create a Repository on GitHub:
- Log in to GitHub and create a new repository.
- Add Remote Repository:
- In the VSCode terminal, run:
git remote add origin https://github.com/yourusername/your-repo-name.git
- In the VSCode terminal, run:
- Push Local Commits:
- Push your commits to GitHub:
git push -u origin master - Replace
masterwithmainif your repository uses themainbranch.
- Push your commits to GitHub:
- Create a Repository on GitHub:
-
Regular Workflow:
- Pull Updates:
- Before starting work, ensure you have the latest changes:
git pull origin master
- Before starting work, ensure you have the latest changes:
- Make Changes:
- Develop your site, make edits in VSCode.
- Stage and Commit:
- After significant changes, stage and commit with descriptive messages.
- Push to Remote:
- Regularly push commits to the remote repository to back up your work and collaborate.
- Pull Updates:
Additional Tips for an Effective Workflow
-
Use Branches:
- Create branches for new features or experiments to keep the
master/mainbranch stable.git checkout -b feature/new-feature
- Create branches for new features or experiments to keep the
-
Merge and Resolve Conflicts:
- After completing work on a branch, merge it back:
git checkout master git merge feature/new-feature - Handle any merge conflicts that arise.
- After completing work on a branch, merge it back:
-
Commit Often with Clear Messages:
- Small, frequent commits with descriptive messages make tracking changes easier.
-
Backup Your Database:
- Use plugins like WP Migrate DB or All-in-One WP Migration to handle database migrations between environments.
-
Automate Tasks:
- Utilize tools like Gulp or Webpack for task automation (e.g., compiling Sass, minifying scripts).
-
Use Environment Variables:
- Manage sensitive information and configurations using environment variables. Consider using a
.envfile with appropriate.gitignorerules.
- Manage sensitive information and configurations using environment variables. Consider using a
-
Collaborate Effectively:
- Use GitHub issues and pull requests to manage collaboration and code reviews if working in a team.
-
Stay Updated:
- Regularly update WordPress core, themes, and plugins to benefit from the latest features and security patches.
Conclusion
By following this tutorial, you’ve set up a robust local WordPress development environment using MAMP and Git. This setup not only facilitates efficient development but also ensures your work is version-controlled and easily manageable. Leveraging tools like VSCode and GitHub enhances collaboration and maintains a clean workflow, whether you’re working solo or as part of a team.
Remember to regularly commit your changes, keep your .gitignore updated, and back up your database to safeguard your work. Happy developing!