How to use WordPress with Git efficiently

I use to work with frameworks like Symfony, Angular, Laravel and more.

When it comes to VCS (Version Control System), we need to remind some facts.

WHAT ARE THE ADVANTAGES OF VCS
1. Keep track of changes by owners, time…
2. Easily roll back changes when necessary
3. Easier to collaborate with other developers

WHAT IS NOT VCS MADE FOR
1. Replacement for scheduled backups, VCS !== BACKUP !!!
2. Debugging. Since every little change is tracked, it’s not its purpose.
3. Committing untested or broken code to the repository… Just a matter of sense !
4. Committing generated content (from user or otherwise)
5. Uploads & thumbnails => heavy repo with absolutely no benefit, use a cdn for media content !

WHAT IS THE GOAL OF VCS WITH WORDPRESS (Like another framework or cms)
1. Keep sensitive information out of the repository
2. Avoid wordpress core and upload files

Ok. You will surely tell me that this way, it will be a pain to retrieve the full project for a new developer, because there will be no wordpress core files, nor wp-config.php file.

Sure you’re right. But remember, VCS !== BACKUP, keep third parties you are not allowed to touch apart of your repository.
ANND, I will show an easier way to bypass this simple issue.

  • It is not a good way to modify wordpress core (what about future updates)? Don’t keep core files inside your repository.
  • Config file is personal to each user, never shared it on vcs, keep database datas secret.

NOW, LET’S START !!!!

Let’s start with the .gitignore file content

# Ignore everything in the root except the "wp-content" directory.
/*
!.gitignore
!wp-content/
!.htaccess

# Ignore everything in the "wp-content" directory, except the based themes and the "tmp" directory.
wp-content/themes/twenty*
wp-content/tmp/

#Do this essentially if you store your media content inside a cdn
wp-content/uploads/ 

# Hidden files
*.DS_Store
*Thumbs.db
*.sass-cache*
*~imageoptim*

Now, let’s grab the wordpress core and config files. For that we will need “wp-cli” extension, you can download it here: https://www.howtoforge.com/tutorial/getting-started-with-wp-cli/

Once, you have “wp-cli” enabled in your cli environment, we can start the fun:

  1. Install WordPress core files with a specific version
    wp core download --version=x.x.x --allow-root
    
  2. Install config file with database datas

    wp core config --dbname=mysite --dbuser=root --dbpass=password --dbhost=127.0.0.1
    

And here we go, quite simple, enjoy 🙂

How to use WordPress with Git efficiently

Git clone and pull from command line without prompt

If you have ever tried to clone or pull a private git repository from command line, you surely face the prompt for username and password.

This is a quick way to solve this problem:

git clone https://username:password@github.com/username/repository.git

This command also work for git pull

git pull https://username:password@github.com/username/repository.git

It saved me a lot of time to monitor a git repository from command line, I hope it will help you.

Git clone and pull from command line without prompt

Github – Normalize repository line endings on both unix and windows

When all the members of your team assigned to a project are working on the same OS, everything goes fine, but when they are using different OS such as Linux and Windows, you will surely encounter line endings issues in the different commits, that will degrade comparisons tasks.

The solution by example, if your production server is under linux, is to tell your developers under windows to force Github and their IDE apply “LF” line endings (if you use netbeans, check for “show and change line endings”). We will see here how to force Github using LF line endings on Windows (I assume your IDE is configured to save your files in LF line ending).

First, open your console and type

git config --global core.autocrlf false

Go to Github directory


C:\Program Files (x86)\Git\etc

//Give write permissions (if necessary) to these files and open them
.gitattributes
.gitconfig

//In .gitattributes add this line, files you will check will now be normalized
* text eol=lf

//In .gitconfig, set autocrlf to false if it hasn't been done by the previous command
//This option will not set automatically files to Crlf even if in lf before
[core]
    symlinks = false
    autocrlf = false

 

Open your console and apply the generalization to Github core

git config --global core.eol lf

 

Now, every file you will work with, will be cached and committed with LF line endings.
But, maybe you want all your repository and its branches to be updated to correct line endings, right?

Let’s do this, open your console one more time and type this:

//These commands will remove cached files and normalise the current repo branch
git rm --cached -rf .
git diff --cached --name-only -z | xargs -0 git add .

//these ones will normalize all the repo branches
git ls-files -z | xargs -0 rm
git checkout .

That’s all, you will no more have line endings issues in your project.
On the other side, make sure that new developers integrating your team will follow this process.
PS: If no changes appear, reclone your repository.

Good work guys.

Github – Normalize repository line endings on both unix and windows