Drupal Setup with Git 2024

by | Nov 7, 2024

Build Drupal 10 website

with Git

Create a Drupal (or other) website locally or online with an included Git environment for improved further management

Starting source: https://www.drupal.org/docs/getting-started/installing-drupal/building-a-drupal-site-with-git

=> Provides full info for creating Drupal website, managing, adding modules,… and update the git repo along with.

Prerequisites: composer, drush, … check other tutorials for drupal.

 

Working online on distant server: Creating the Central Repository.

My goal is to update my PapierLogik website by creating a new one under git management directly on the distant server. I used sh to access my distant server on OVH.

From there I created a new main folder at the root of my server named drupal10-papierlogik. Then I follow the above source indications to setup the git environment in this folder and future sub-contents:

(On your remote server)

$ cd ~
$ git init --bare drupal10-papierlogik.git 

1Working locally: Locally Cloning Drupal

When working on local server like with WAMP in my case, you will setup the repo in a local folder on your computer or your local server while cloning the git repo that already exist for drupal10.

 

You could follow these steps on the server as well. Let’s clone Drupal (version 10) to create a local development environment.

(On your local development environment)

$ git clone http://git.drupal.org/project/drupal.git drupal10-local-papierlogik
$ cd drupal10-local-papierlogik
$ git checkout 10.x 

 The cloning takes some minutes, and when done you get this:

 

Your I counted on installing drupal 10 but the instructions followed were wrritten at the time of drupal 7. Then I was not able to run a checkout 10.x yet… Drupal 10 may be too recent for a drupal10 repo ready yet on Nov 2024.

To be continued…

 

I found out how to solve this issue later on while going to this Drupal page:
https://www.drupal.org/project/drupal/git-instructions

It has a rolling menu displaying all the Drupal branch names one’s cloned. I replaced 10.x by 10.0.x (or later) in my checkout instructions, which setup the cloned files to the 10.0.x branch.

At this time of writing, the stable advised version seems to be 10.3.7 so I may have to run updates if starting from 10.0.x instead of directly 10.3.x.

 

 Updating Remotes

It now takes to update the default remotes info of the cloned repo to reflect that you won’t be pushing to drupal.org with your project’s code (the drupal.org Drupal project repository) .

You can rename the original origin remote to ‘drupal’ and create a new origin pointed at the bare repository you’ve created on your server.

(On your local development environment)

$ git remote rename origin drupal
$ git remote add origin path/to/your/central/git/repo

(example: ssh://cellucomvi@ssh.cluster003.hosting.ovh.net:22/drupal10-local-papierlogik/drupal10-local-papierlogik.git)

To see a list of your remote repositories, run the command:

$ git remote

For a more detailed listing that includes the remote repositories’ URLs, add a -v flag (for verbose) to the end of the command:

$ git remote -v

Creating a Working Branch and install drupal

Now, you need a branch where you can track not only Drupal core, but also all of the contributed and custom modules and themes for your site. Create a branch using the command:

(On your local development environment)

$ git checkout -b drupal10-local-papierlogik

This command creates a new branch named drupal10-local-papierlogik and checks it out. It is equivalent to running the commands:

$ git branch drupal10-local-papierlogik
$ git checkout drupal10-local-papierlogik

You can use this drupal10-local-papierlogik branch as a working branch to add contributed and custom modules and themes to your site. Consider it the equivalent of the default Git ‘master’ branch for your project.

At this point, you should complete the Drupal installation process to get a working local installation. For Drupal 8 or higher, you’ll need to do ‘composer install’ in the Drupal site root before running the web installer:

$ composer install

 


Setting up the .gitignore file to exclude sensitive files

There are a few things that you probably do not want to have tracked in the repository, namely sites/default/files and sites/default/settings.php  because it contains sensitive database access information and will be different on each environment.

One way to tell Git to exclude certain files and directories from the repository is to set up a .gitignore file.

Drupal 8 or higher comes without a .gitignore but has an example.gitignore from which you can create a .gitignore. If you wish to transfer configuration between sites using git, you probably need to change the location of the config directory (which is set in settings.php) to one which is tracked by git. By default, the config directory is in sites/default/files, and by default the .gitignore which you create from example.gitignore will exclude this directory path.

 

FYI, the settings in the Drupal 7 default .gitignore file are as follow:

# Ignore configuration files that may contain sensitive information.
sites/*/settings*.php

# Ignore paths that contain user-generated content.
sites/*/files
sites/*/private

 To customize these gitignore settings, reger to the drupal source on top of this page.

Pushing Code to the Central Repository and Completing Initial Deployment

Now, you can push your code up to the origin remote on your server:

(On your local development environment)

$ git push origin drupal10-local-papierlogik

This command copies your local branch drupal10-local-papierlogik to a branch of the same name in your remote repository origin.

You can now provision your other tiers with this code from the repository. Log into your server and provision a development environment from the code you’ve committed:

 

 

(On your remote server)

$ git clone --branch fooproject ssh://fooproject@fooproject.com/home/users/fooproject/fooproject.git fooproject_dev

Now, you have a fooproject_dev directory that you can use as the root of a new virtual host. You can proceed through the normal Drupal installation process using this development copy of your site and a separate database for it. Repeat this process for the Staging and Production environments- we’ll assume that they live on the same server in directories fooproject_stg and fooproject_prod.

 

 

Adding Contributed Modules and Themes

Check the source

 

Updating Drupal Core

Check the source