Part one of this tutorial dealt with getting a local Drupal up and running. In this tutorial we will deal with using Git to update and manage your live site on a shared web hosting platform.
The recommend way is to setup a repository that contains our master copy of your Drupal site and the easiest way is to use Github for this. We can then clone all files and directories from your Github repository to your shared hosting platform where the live site will resides. Any modified files in the repository can be pushed to live site. You can do all the changes on a local installation site and push your new changes to your Github repository which can be then pull in on your live site.
Make sure Git is installed on your local machine. If not, follow this tutorial: Getting started - Installing Git.
Almost all web hosting providers will already have Git on their shared hosting. If not, you will need to find a hosting provider that offers Git on all their shared hosting servers. For example, A2Hosting already offers Git on all their shared hosting servers.
Next, setup a free account on Github.com
Click on the 'New' button to create a new repo.
On the next screen, enter a name for your repository. Something like 'drupal' should be sufficient and set it to private so that only you or anyone else (in a team) can access. Click on 'create repository' button.
Now that we have a new repository setup, we need to add our full Drupal installation files and directories into this repo (we named it 'drupal').
Open up a terminal and change directory to where your Drupal installation is. If you remember from the part 1 of this tutorial, i named it /test So we type:
You may have named your directory different. Replace 'test' with whatever directory name you have given to your Drupal installation project.
Once inside that directory, we will create a README txt file to be used show any instructions and notices we want to present on the github repo. More on this later.
echo "# drupal" >> README.md
Next, we initialise this directory to setup a new empty repository to be used as the project.
At this point before we add all the files and directories to our repository, we want to exclude certain file and directory because they are unique to each domain and we don't want to clutter up our repository. The directory to exclude is /web/sites/ because this directory contains cached files, content images and settings.php
These files can be very different from your local and live site. What we will do later is to back them up separately.
Still in your same directory where you initialised it, we create a gitignore file:
Open up .gitignore file:
Add the following to your .gitignore file:
# Ignore directories generated by Composer. /vendor/ # macOS Files .idea/ .DS_Store # Ignore Node modules. node_modules/ #Ignore log files logs/ *.log # Ignore configuration files that may contain sensitive information. web/sites/*/settings*.php web/sites/*/services*.yml web/sites/*/default.settings.php # Ignore paths that contain user-generated content. web/sites/*/files web/sites/*/private # Ignore SimpleTest multi-site environment. web/sites/simpletest # Ignore sourcemaps. sourcemaps/ # Ignore .env files as they are personal. .env
To save the file, hold down crtl key and the letter o key. To exit out of Nano, hold down ctrl key and the letter x key.
Drupal recommends to ignore these directories that are managed by Composer:
# Ignore directories generated by Composer. /drush/contrib/ /vendor/ /web/core/ /web/modules/contrib/ /web/themes/contrib/ /web/profiles/contrib/ /web/libraries/
I am guessing the reason is that you are supposed to run composer on your live site to fetch all these files. But in our case, we wouldn't be running composer on shared hosting platform. So i included all of them to be committed into the remote master repository. Only /vendor is ignored because these vendor 3rd party dependencies can change quite frequently and it's easy enough just to remember to copy it over to live site.
It's also recommended to leave a note in your README.md file for setup instruction on both local and live site as a reminder. Please do not leave any sensitive information (such as user and passwords) in this file because it can easily be viewed in the browser or downloaded from your live site.
Something like this, in your README.md file (it uses Markdown's syntax):
# Setup instructions for a working repo to get started Once you have installed Drupal and created an initial repo (master) and committed all the files and directories to remote repo. Next step is to backup web/sites/default and /vendor Each time you setup a new clone site to work with, follow the steps below: ## Local site (development) - clone the remote repository: git clone https://github.com/[user name]/[repository name].git - Copy /vendor from backup to webroot - Copy /files directory from backup to local site in web/sites/default - Copy settings.php from backup to local site in web/sites/default. - Update your settings.php DB login credentials - Update trusted host (ie, ($settings['trusted_host_patterns'] =) in settings.php to match domain name - Run composer to fetch all other files (ie, vendor): composer update ## Live site (production) - clone the remote repository: git clone https://github.com/[user name]/[repository name].git - Copy /vendor directory from local to live site's webroot (ie, /public_html) - Copy web/sites/default/files from local to live - Copy settings.php from local to live and change the DB login credentials that is used on live site.
That was just an example. It's up to you what notes you had that makes sense to you or better fit your development workflow.
If you want to know more about .gitignore file, read this article: How to use a .gitignore file
Next, we want to commit all the files and directories to the repository:
git add . && git commit -m "Initial Commit of all Drupal files and directories"
Above executes 2 separate commands. First to add everything in that directory and then we commit it with a message. Be patient, it will take some time to complete (depending on the speed of your computer).
Next, we need to add all the commits to our remote repository (ie, on Github). We do this with this command:
git remote add origin https://github.com/[username]/drupal.git
Replace [username] with your own.
Finally, push your changes to remote repository (ie, on Github) which is the master. We do this with this command:
git push -u origin master
You will be asked for your username and password. This is your username and password you used to signup on Github.com
Once completed, if you type:
you should see a message:
On branch master Your branch is up to date with 'origin/master'. nothing to commit, working tree clean
That means everything is fine so let's go check out our repository on Github. Visit github.com and if you are logged in, you will see your new repository in the left panel.
Remember the README.md file I talked about earlier, Github use this file to present any info on the repo page. This is useful if you have any instructions or reminders to add that will show up on the repo page. This file can also be edited on github so you can modify it any time to update your info.
Here's a PDF guide on how to edit the README.md file
For the final part of this tutorial, we will clone the repository to live site.
- Installing and updating Drupal 9 on shared web hosting platform for Mac user (part 1)
- Installing and updating Drupal 9 on shared web hosting platform for Mac user (part 2)
- Installing and updating Drupal 9 on shared web hosting platform for Mac user (part 3)
- How to handle Git submodules in my Drupal repository