How to become a web developer - Part 3

Published: Jan 22, 2015 by C.S. Rhymes

One of the biggest lessons I learnt was to make sure you have all your code backed up. When working on a project you may add and remove lots of lines of code in a couple of hours and its easy to make a mistake and delete the wrong line and for some reason the all mighty UNDO funtion doesn’t bring it back.


So how can you back up your code to ensure you can get back anything you may delete by mistake? Personally, I use Git. Git is described as:

Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.

Git allows you to work on your code locally, commit changes, roll back changes, push your changes to a repository and pull your code onto your web server.

This is a really good workflow as it ensures that when you start your local development it is using exactly the same code as your web server. You then commit your changes to git, along with a commit message detailing what you have changed, merge them in (or create a pull request) and then push them to your repository. The repository is where you code is stored.


There are great and easy to use online services such as bitbucket and GitHub where you can store your repositories in the cloud. Lets call them repos as I can’t be bothered to type repositories again (I know I just did…). There are both public and private repos available. Public are designed for sharing and working with others and private are for code you don’t want to share. There is normally a charge for creating private repos so check before you create your account with your chosen service.

Github also offers an awesome pages service where you can host your website for free (which is what I use to host this site).

Anyway, once the code is safely in the repo you can then pull the latest code from your web server to update your changes.

Branching and Merging

The other great thing about Git is that you can create branches of your code. This means that you can work on a bug fix or enhancement on a separate branch to the code running on your website, make your changes on that branch and then merge the changes back into your live (or master) branch.

This also means that you can work on different projects at the same time. For example, you cna be working on a branch to add a new menu to your site, but in the mean time you may notice a bug with your contact form. You just create a new branch for your bug fix and work on that branch, but it means that you don’t need to push any changes to your new menu live at the same time as your bug fix. The menu code can site in its own branch, not affecting the bug fix.

Once the work on the branch is complete you can merge your branch into your master branch. Git detects changes to files, line by line and character by character. Therefore if there are any conflicts between what you are merging in and the existing code it will tell you. You can then merge in whichever version of the code is correct. This ensures you don’t automatically overwrite anything.

Pull Requests

An alternative workflow might be to create a pull request, rather than just merging in your changes directly. This is basically asking for permission to merge your changes in. This is especially useful if you are wotking with a team as it allows others to view your changes and test out your code and flag any potential issues before the code is merged. It may seem a bit over the top if you are working on your own but is quite a good workflow to follow as it ensures you check your changes before merging.


Another useful feature of Git is that it allows you to fork a repository. Forking allows you to create your own copy of a repo and work on it without affecting anyone else. This allows you to build on what someone else has done and enhance it to suit your own needs. If you have fixed an issue with a forked repo you can create a pull request to merge your fork back into the master repo.

Getting started

There is quite a lot to learn about Git and finding the correct workflow for you. Hopefully I haven’t scared you off and you see the benefits of using version control. Its definitely something you want to get into the habit of using and you will probably experience a scenario like the one mentioned above, where you want to roll back or undo your changes, and think to yourself “I’m soooooo glad I’m using version control”.

In my next post I’ll talk a bit about using frameworks to get you off to a quicker start.


Latest Posts

The PHP for loop
The PHP for loop

If you have something you want to repeat then consider using a PHP for loop, saving you from copying and pasting the same code multiple times. Adding the code into a loop means you only have to write it once, and you also only have to maintain the code in one place in future.

Refactoring to Laravel 8 Class Model Factories
Refactoring to Laravel 8 Class Model Factories

Laravel 8 introduced new class based model factories and if you have an existing project you can use the legacy factories package to keep using the old factories. I have tended to keep the factories as they were and continue development, but after working on a fresh Laravel 8 project and using the new class based syntax I decided to go back and update the factories in the older Laravel apps. This article explains how I went about refactoring the factories to classes.

The PHP array
The PHP array

Arrays are a useful toolt to store multiple values. You can access a specific value in an array using a key. By default, if you don’t specify keys, they will be numeric and start at zero (not one);

How NOT to make a website

How NOT to make a Website

By C.S. Rhymes

From £8.99

Nigel's Intranet Adventure

Nigel's Intranet Adventure

By C.S. Rhymes

From £2.69