Dave McCourt thinks some thoughts...

Git basics

Posted in: Articles, Technical, WordPress

Git is a tool for recording and storing changes to code, so that you can keep track of the changes you make. And if your code breaks you can easily roll back to an earlier working version. Git is usually set up to work locally so you need a local development environment, and you can optionally push your Git repo to a remote repository and then to your production server.

Getting started

Install Git if you haven’t already.  Set-up your details globally for your system, so you don’t need to enter them every time you use Git.

$ git config --global user.name "John Doe"
$ git config --global user.email johndoe@example.com

Using Git

Git runs in the directory you work in, so you need to initialise Git for each project you want to track.

$ cd /Users/davem/Documents/Dropbox/Work\ sites/test-site

Let’s set-up a Git repository or repo:

$ git init

Git says:

Initialized empty Git repository in /Users/davem/Documents/Dropbox/Work sites/test-site/.git/

All of the changes are tracked in the .git files. Add files using your usual text editor.


$ git status

Git will then show you a list of files it has spotted but it doesn’t know about as they aren’t in its index. Files are added using:

$ git add --all

These files are then staged but still need to be committed to Git’s index:

$ git commit -m "Initial Commit"

The -m bit adds the comment between the quotes. It is a good habit to get into to make these comments useful and descriptive so you can easily see what was added or changed. The add and commit steps can also be combined:

$ git commit -am "Your message"


You can exclude any files you don’t want in the repo with the .gitignore file.

Using Bitbucket

Bitbucket allows you to store your repo online. What is great about it, is it allows unlimited free repos so I store all my sites there. Once you’ve committed your files, you can push your repo:

$ git remote add origin https://USERNAME@bitbucket.org/USERNAME/GitSample.git
$ git push -u origin master

 Using Git with remote servers

Login to your remote server by SSH:

$ ssh username@myserver.com
$ git status
$ git clone https://USERNAME@bitbucket.org/USERNAME/my-project.git

If the directory you are cloning into isn’t empty, use a dot like so:

$ git clone https://USERNAME@bitbucket.org/USERNAME/my-project.git .

 Git and WordPress

Create a wp-config-local.php file to hold the local development settings:

// Local server settings

// Local Database
define('DB_NAME', 'client');
define('DB_USER', 'root');
define('DB_PASSWORD', 'root');
define('DB_HOST', 'localhost');

// Overwrites the database to save keep editing the DB

// Turn on debug for local environment
define('WP_DEBUG', true);

Then in wp-config.php we have:

// Use these settings on the local server
if (file_exists( dirname( __FILE__ ) . '/wp-config-local.php')) {
  include( dirname( __FILE__ ) . '/wp-config-local.php' );
// Otherwise use the below settings (on live server)
} else {
  // Live Server Database Settings
  define('DB_NAME', 'client');
  define('DB_USER', 'client');
  define('DB_PASSWORD', 'password');
  define('DB_HOST', 'localhost');
  // Overwrites the database to save keep edeting the DB
  // Turn Debug off on live server
  define('WP_DEBUG', false);

// Usual WordPress config

Some good tricks for things to ignore:

# Don't add the local settings file to the repository #
# No need to add the upgrade or uploads folders to the repo #
# No need for these #