系统管理员 GIT 指南

来自MoodleDocs
Yucheng Hu讨论 | 贡献2013年8月29日 (四) 02:46的版本
跳转至:导航、​搜索

这个页面用来描述如何在你安装 Moodle 的服务器上使用 GIT 来进行更新。

如果你自定制了 Moodle 的核心代码,建议参考下面的指南:Moodle 开发快速 Git 指南

希望能够比较熟练的使用 GIT,你需要熟悉一些概念,请参考下面的章节。

如果你从来没有接触过 GIT 或者你使用过其他版本的版本控制服务器,例如 CVS 和 Subversion,这里还是有点学习曲线的。

获得 Git 命令行运行工具 (Windows, OSX, Linux 和其他类型操作系统)

Git 版本控制服务器现在也越来越被广泛的使用了,最常用的是在 Linux 服务器上,但是目前 Git 也能够被主流操作系统支持了:

当你在你使用的操作系统中下载和安装好 Git 后,本页面使用的 Git 命令行也应该能够被正确使用。

Obtaining the code from Git

The command line version of Git is discussed here. Graphical clients are little more than wrappers around the command line version, so you should be able to deduce the correct parameters quite easily.

You can find the official Moodle git repository at git://git.moodle.org/moodle.git (with an official clone at git://github.com/moodle/moodle.git). To initialize your local checkout, use

$ git clone git://git.moodle.org/moodle.git                       (1)
$ cd moodle
$ git branch -a                                                   (2)
$ git branch --track MOODLE_25_STABLE origin/MOODLE_25_STABLE     (3)
$ git checkout MOODLE_25_STABLE                                   (4)
  • The command (1) initializes the new local repository as a clone of the 'upstream' (i.e. the remote server based) moodle.git repository. The upstream repository is called 'origin' by default. It creates a new directory named moodle, where it downloads all the files. This operation can take a while as it is actually getting the entire history of all Moodle versions
  • The command (2) lists all available branches.
  • Use the command (3) to create a new local branch called MOODLE_25_STABLE and set it to track the remote branch MOODLE_25_STABLE from the upstream repository.
  • The command (4) actually switches to the newly created local branch.

Note that Git has a huge number of options for each command and it's actually possible to do the above process with a single command (left as an exercise!!).

Git from behind a firewall

Git uses a read-only protocol that may be blocked by your firewall (port 9418). If this is a problem, you can use Github's http version https://github.com/moodle/moodle.git. It's a bit slower, so use the Git protocol if you can.

Updating your installation

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. Check the shortlog to see if the official repository has been already updated or not.

To update your code to the latest version (on the MOODLE_22_STABLE branch) all you have to do is:

$ cd /path/to/your/moodle/
$ git pull

If this is a production site you should still consider the Upgrade instructions (e.g. take backups).

Installing a contributed extension from its Git repository

This is one way to handle adding plugins from other Git repositories into your Moodle repository. Another way is to use Git Submodules. However, at the time of writing, this is one of Git's rougher features and should be regarded as an advanced option.

For example, let us say we want to install the Book module from its Git repository into our Moodle 2.2.

$ cd /path/to/your/moodle/
$ cd mod                                                          (1)
$ git clone git://github.com/skodak/moodle-mod_book.git book      (2)
$ cd book
$ git checkout -b MOODLE_22_STABLE origin/MOODLE_22_STABLE        (3)
$ git branch -d master                                            (4)

The command (1) changes the current directory into the mod folder of your local Moodle clone. The command (2) creates a new subdirectory book and makes a local clone of Petr Škoda's vanilla Book repository. The command (3) creates a new local branch that will track the remote branch with a Book version for Moodle 2.2. The command (4) deletes the master that was created automatically by git-clone in (2) as we do not want it in this production checkout.

Note: you should check first the compatibility of a module with your Moodle branch by asking directly to the Maintainer before cloning the repo or - if you want to guess it - by issueing the command below before running the command (3), in order to verify what is available among the branches:

$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/MOODLE_15_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/master

This will avoid an error message when you issue the command (3) against a nonexistent branch, e.g.:

§ git checkout -b MOODLE_25_STABLE origin/MOODLE_25_STABLE
fatal: git checkout: updating paths is incompatible with switching branches.
Did you intend to checkout 'origin/MOODLE_25_STABLE' which can not be resolved as commit?

In this case you should ask the Mantainer, here Petr Škoda, if master is the correct branch for Moodle 2.3+ and he will reply that the Book module has been already integrated in Moodle starting from 2.3 so there's no need to install it usit git. Please, as a general rule, before asking the Mantainer for something you should check the online documentation.

Now it is wise to put the new directory mod/book/ to the list of ignored files of the main Moodle clone, otherwise a status of the main clone will keep reminding you that the new code has not been checked in.

$ cd /path/to/your/moodle/
$ echo /mod/book/ >> .git/info/exclude

To update your Moodle installation now, you must visit both Git repositories and pull changes from upstream.

$ cd /path/to/your/moodle/
$ git pull
$ cd mod/book
$ git pull

Writing a shell script with these lines in the root of Moodle installation is a very good idea. Otherwise it is easy to forget what Git repositories are there within the main Moodle repository.

See also

Moodle Docs
Moodle forum discussions
External resources