Use PHP with Git Tutorial (with best practices)

Learn how to use PHP with Git Tutorial.

Share This Post

Share on linkedin
Share on facebook
Share on twitter
Share on email

PHP allows you to create web apps that run pretty much anywhere, including on cheap hosting. Instead, git allows you to keep track of the changes in your source code. You can use Git in your PHP projects to leverage the best of both. In this post, we’ll cover some best practices when it comes to using PHP with Git. This quick PHP with Git tutorial will help you create your project, and also understand what you shouldn’t do.

A quick intro on git

If you are here, probably you are somewhat familiar with git. In case you aren’t, git is a simple mechanism to keep track of your code. You need a git server (like GitHub), where you push your source code. Then, you work on your PHP code on your local PC, and when you make some progress you upload it to the server. To do that, you use git. Furthermore, the server is not just a dumb file-share. It is smart enough to keep versions so that you can rollback to the previous version if needed.

PHP with Git tutorial, how to use git to implement best practices in your PHP project. Here the Git Logo.
The git logo.

Start using Git is easy, but you need to download Git from here first. However, you should use your brain when you use git. Not all files should be part of your commit and push (upload to the server). In this PHP with Git tutorial, we specifically see how to integrate git with PHP.

What is .gitignore?

In the root folder of your project, you should find a file named .gitignore. That’s a simple text file indicating what other files are not part of your project. Then, git will ignore such files and never upload them to the server. Just note that you need to create .gitignore first. If you try later to exclude a file that is already part of your project, it won’t work. If that’s your case, simply add the file name you want to exclude in your .gitignore, then remove and recreate the file.

PHP with Git tutorial

Starting out

The first thing you need to do is installing git. You can download it from here, and then execute its installation. Git is available on virtually any platform. Then, add git to your PATH environment variable if the installer didn’t do that for you. Now, you are ready to go.

In your prompt command, navigate to the root folder of your project. Once inside, init your repository with the following command.

git init

This will create a .git folder that Git needs to use to do its job. Now, create (outside of that folder) your .gitignore file. At this point, you are set to go. You can import your repository in your favorite UI git client, like GitHub desktop.

The core of our PHP with Git tutorial is understanding which files you should put in your .gitignore, and which not. Below, the best practices.

Include in the project (NEVER put in .gitignore)

Here are the files that you want to upload to your server all the time, with an explanation for each. Since you want to include them in the project, you don’t want git to ignore them. Don’t put them in .gitignore.

  • Your source code – this is your project, of course you don’t want to ignore it.
  • composer.json – this file lists all the dependencies of your project. You want the ability to rollback to a previous version of your project and restore the dependencies you had back then. Must have!
  • composer.lock.json – PHP automatically generates this file from composer.json. Even if the rule of thumb is to exclude from your project all auto-generated files, this is an exception. You should always include this file in your project. Why? It lists the complete dependency tree of your project. In other words, you may require a module, which then requires another module on a specific version. You want to be able to roll-back to a specific version of your project, with its full dependency tree. Otherwise, you may end up with a broken project because a third-party has updated its own dependencies.
  • .env.testing – If you have this file, it is because it describes the test environment. Normally, such an environment is for self-contained tests. Nice idea to share it and upload it to your server.
  • Unit and feature tests – Any other developer getting the code from the server should be able to run tests to see if the code works properly on his own machine. Thus, include them in the project.

Exclude from the project (put in .gitignore)

And now, the files that should not be part of your project, and that you should add to .gitignore. To add them, simply add the file path (relative to the project root). Add one path per line in .gitignore. This is the other important part of this PHP with git tutorial.

  • Files generated automatically – if they are not composer.lock.json!
  • .env – this files describes the environment of your machine. If you have it, it describes your own machine, and should not be part of the project.
  • IDE Settings – If you are using an IDE to edit your file, it will probably create a setting folder inside your project. That’s something between you and your IDE, and not really part of the project. Otherwise, another developer pulling the code will have his own settings overridden by yours. Frustrating!

A quick wrap-up

In conclusion, setting up git is extremely easy, also for PHP. However, we used this PHP with Git tutorial to cover the best practices, specifically on which files to include or ignore from your project. In short, everything that describes specifically your project goes into the source code, everything else goes into .gitignore.

Do you think this approach helps you managing your PHP project? Do you want to learn more about Git? Let me know in the comments.

Alessandro Maggio

Alessandro Maggio

Project manager, critical-thinker, passionate about networking & coding. I believe that time is the most precious resource we have, and that technology can help us not to waste it. I founded ICTShore.com with the same principle: I share what I learn so that you get value from it faster than I did.
Alessandro Maggio

Alessandro Maggio

Project manager, critical-thinker, passionate about networking & coding. I believe that time is the most precious resource we have, and that technology can help us not to waste it. I founded ICTShore.com with the same principle: I share what I learn so that you get value from it faster than I did.

Join the Newsletter to Get Ahead

Revolutionary tips to get ahead with technology directly in your Inbox.

Alessandro Maggio

2019-10-17T16:30:23+00:00

Unspecified

PHP

Unspecified

Want Visibility from Tech Professionals?

If you feel like sharing your knowledge, we are open to guest posting - and it's free. Find out more now.