Web development and useful information

Nathan's Blog

Using Git with Moodle

New to Git? Learn it in 15 minutes here.

Working on Moodle with Git is a breeze!
Previously, applying security updates to our Moodle 1.9 site was a ballbreak because we’d changed so much core code. I’d have to go through and apply fixes individually to each updated file to make sure new changes didn’t clash with our custom changes.

We now maintain a forked copy of Moodle on our college’s GitHub account.
Maintaining a copy of Moodle in our own GitHub repository allows us to easily update, manage changes (add our theme, blocks, modules) and contribute bug fixes.

It took me a while to realise that with Git, we can maintain our own version (branch) of Moodle, based on the latest STABLE release. We can keep our added blocks, modules and custom theme separate from STABLE branches. We’re only adding files to our branch and not changing core files, so getting and merging with latest stable updates is easy.

These pages do a great job of explaining Moodle development with Git:

Installing Moodle using Git

  1. Sign in or create your GitHub account – free for public repositories.
  2. Download and install Git on your server. Instructions here.
    Note: Port 22 (SSH) is blocked and a proxy is used at the College I work at. I followed these steps to use Git on port 443 through our proxy.
  3. Browse to Moodle’s GitHub repository – https://github.com/moodle/moodle
  4. Click ‘Fork’ in the top right of the web interface.
  5. Clone your forked repository locally into a folder on your server. Change ‘username’ below to your GitHub username.
    # Clone our fork into a directory called 'moodle'
    git clone git@github.com:username/moodle.git moodle
  6. Configure remote repositories.
    Add ‘upstream’ to track changes to the official moodle repository.

    # Change the active directory in the prompt to the newly cloned 'moodle' directory
    cd moodle
    
    # Assigns the original repo to a remote called 'upstream'
    git remote add upstream git@github.com:moodle/moodle.git
    
    # Pulls in changes not present in your local repository, without modifying your files
    git fetch upstream
    
    # Creates a new branch called MOODLE_23_STABLE
    # sets it to track the remote branch MOODLE_23_STABLE from the upstream repository
    git branch --track MOODLE_23_STABLE origin/MOODLE_23_STABLE
    
    # show all branches
    git branch -a
    
    # should show:
    MOODLE_23_STABLE
    * master
    remotes/origin/HEAD -> origin/master
    remotes/origin/MOODLE_13_STABLE
    remotes/origin/MOODLE_14_STABLE
    remotes/origin/MOODLE_15_STABLE
    remotes/origin/MOODLE_16_STABLE
    remotes/origin/MOODLE_17_STABLE
    remotes/origin/MOODLE_18_STABLE
    remotes/origin/MOODLE_19_STABLE
    remotes/origin/MOODLE_20_STABLE
    remotes/origin/MOODLE_21_STABLE
    remotes/origin/MOODLE_22_STABLE
    remotes/origin/MOODLE_23_STABLE
    remotes/origin/master
    remotes/upstream/MOODLE_13_STABLE
    remotes/upstream/MOODLE_14_STABLE
    remotes/upstream/MOODLE_15_STABLE
    remotes/upstream/MOODLE_16_STABLE
    remotes/upstream/MOODLE_17_STABLE
    remotes/upstream/MOODLE_18_STABLE
    remotes/upstream/MOODLE_19_STABLE
    remotes/upstream/MOODLE_20_STABLE
    remotes/upstream/MOODLE_21_STABLE
    remotes/upstream/MOODLE_22_STABLE
    remotes/upstream/MOODLE_23_STABLE
    remotes/upstream/master
    
    # switch to the latest stable branch
    git checkout MOODLE_23_STABLE

New branch for your changes

After you’ve switched to MOODLE_23_STABLE you can create a new branch to store any changes you want to make to your chosen STABLE version.

# creates a branch called conel-MOODLE_23_STABLE, based on 2.3 STABLE
git checkout -b conel-MOODLE_23_STABLE

In this example I create a new branch called conel-MOODLE_23_STABLE that will contain our added modules, blocks and custom theme.

After we’ve added files to our local copy of Moodle we want to push these to our conel-MOODLE_23_STABLE branch.

# Adds our changed files
git add .

git commit -m 'add blocks, modules, theme'

# Push your changes to conel-MOODLE_23_STABLE
git push origin conel-MOODLE_23_STABLE

Updating Moodle

With Git, I simply fetch the latest updates from the Moodle repository (upstream), push them to my copy of moodle (origin), pull down the latest changes locally to my server, then merge them with conel-MOODLE_23_STABLE. Because conel-MOODLE_23_STABLE mostly contains new code (avoiding core changes as much as possible) I’ve yet to experience a merge conflict.

Here’s the (currently manual) process of getting the latest Moodle changes into our forked copy.

git fetch upstream

git push origin refs/remotes/upstream/MOODLE_22_STABLE:MOODLE_22_STABLE
git push origin refs/remotes/upstream/MOODLE_23_STABLE:MOODLE_23_STABLE
git push origin refs/remotes/upstream/master:master

# In branch conel-MOODLE_23_STABLE
git checkout MOODLE_23_STABLE
git pull
git checkout conel-MOODLE_23_STABLE
git merge MOODLE_23_STABLE

Notification of Updates

GitHub provides a handy feed of commits to any branch/master.
You can easily subscribe to Moodle commits for any branch you want. At work I use Outlook 2010 to subscribe to MOODLE_22_STABLE and MOODLE_23_STABLE.

The Git for Administrators page says, “The Moodle development team performs integration and testing of fixed bugs every Monday and Tuesday. On Wednesday you can install all patches by updating your code”.

I find this visual reminder useful. I’ve added this feed to my favourites so it’s prominent in Outlook.

Previous

Upgrading to Moodle 2.3

Next

Uses for pegs

4 Comments

  1. Nathan, thanks for this post, this is the best summary I have found for how to manage a minor fork of Moodle with additional modules using git. This was just was I was needing to improve the code management process for a new client that is using Moodle 2.7 with ELIS and a few minor tweaks to Moodle core. I really like how you have boiled this down to just the minimal steps.

    Thanks again!

  2. Ryan

    Thanks for the guide. Just one question: do these branches go into different directories or all the same directory? What is the base directory that I should be using?

    • Hi Ryan,

      You only need one folder, the folder you check Moodle out to.
      When you switch to a branch, the files in this directory change to the files in that branch.

      Your document root is your base directory – the dir which contains the files seen here: https://github.com/moodle/moodle

      Hope this helps.

      Nathan

  3. Conor

    Thanks for the amazing post Nathan – this one really helped me get started with Github and Moodle.

    Just a quick note for anyone following these instructions themselves – Nathan mentions how to use git through port 443 in a link in his instructions. It's important to note that these extra instructions include information on setting up an SSH key for github that you'll need to carry out regardless of the port you're using for github. I missed these the first time round and it took me a little while to work out what I needed to do to get past the "Premission denied (publickey)" warnings.

    Nathan's instructions are pretty good – alternatively you could take a look at github's documentation at
    https://help.github.com/articles/generating-an-ss

    Once again a massive thank you to you Nathan!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Powered by WordPress & Theme by Anders Norén