While climbing the Drupal Ladder, Git gives you easy ways to manage changes to the code: you can apply a patch for testing, tweak the patch, experiment, re-roll it, and get back safely to where you started.
This lesson will help you install Git so that you can use it from the command line. GUI versions also exist, but for basic operations it is generally held that the command line is simpler. When you get instructions on drupal.org, while climbing the Drupal Ladder, or when asking for advice on how to use Git, the instructions will assume a command line.
Some steps are specific to your platform. The following platforms are covered in this lesson:
Installing Git on Windows and Mac
If you use Macports on Mac, you may prefer the following:
$ sudo port install git-core +bash_completion +doc +gitweb +svn
Installing Git on Linux
You can find more instructions for your particular distribution at http://git-scm.com/downloads/linux
On RPM-based systems (RHEL, Fedora Core, CentOS, ...), open Terminal and type:$ sudo yum install git
On apt-based systems (Debian, Ubuntu, ...), open Terminal and type:$ sudo apt-get install git-core
Moving Around on the command line
These are some commonly used commands for moving around from the command line. You should try these out and get familiar with at least these basics so you can use Git effectively from the command line.
$ cd modules
$ cd ../themesBasic Configuration
These next commands are a simple way to update Git's configuration file, .gitconfig. The file is a hidden file that normally lives in your user home directory (e.g. on a Mac, this would be in /Users/myusername/.gitconfig). When you run these commands you won't see any response in the terminal, but your .gitconfig file will be updated.
$ git config --global user.name "Your Name"$ git config --global user.email yourname@example.com$ git config --global core.safecrlf trueDownload a Project Using Git
Now you can start to use Git to download, or clone, code from Drupal.org. First we'll download a module. You will download the code directly to where you run the command from. For a module, you would typically download it into the proper place within a Drupal site, e.g. drupalroot/modules, so you would want to first cd to that directory and then proceed with a git clone command.
cd modules from your Drupal root.
$ ls -a$ git status$ git branch -a$ git log -3Download Drupal Using Git
For higher rungs on the Drupal Ladder, you will want to have a Git repository for Drupal core. You can find the correct Git information for Drupal core in the same way as for projects (http://drupal.org/project/drupal/git-instructions). Doing the simple clone command below will download the cutting edge version of Drupal, sometimes referred to as HEAD, which is currently the Drupal 8 development version. Again, this code will be downloaded to the location you are in on the command line, so typically you will download this in the web root folder of your local web server.
git clone http://git.drupal.org/project/drupal.git
Once you have a Drupal 8 clone, you should always update it to the lastest code before you start new work. You can pull down all of the latest changes from Drupal.org, by moving into your Drupal 8 directory, and then doing a git pull command:
git pull
Comments
mike stewart
Sun, 11/18/2012 - 01:22
Permalink
As an alternative to installing GIT
As an alternative to installing GIT on your system, you can install a pre-built totally configured virtual machine designed to help you contribute to Drupal. It has Apache, MySQL, PHP, XDEBUG, GIT, Drush, and a host of other tools all setup. Checkout the DrupalPro Setup lesson.
Alexander Pyle
Sun, 02/17/2013 - 20:10
Permalink
Alternative only replaces one step of this lesson
The DrupalPro Setup lesson only replaces the install step here and ignores the 'Basic Configuration', 'Download a Project Using Git', and 'Download Drupal Using Git' steps of this lesson.
jrbeeman
Sat, 03/09/2013 - 11:54
Permalink
Add Homebrew Git install instructions
Under "Installing Git on Windows and Mac" it'd be useful to provide Homebrew instructions:
sebi
Thu, 03/21/2013 - 23:08
Permalink
Useful aliases
Here are some useful aliases to setup with git.
git config --global alias.st statusgit config --global alias.co checkoutgit config --global alias.br branchThen you can simply call
git brto display the branch, etc.benjifisher
Wed, 05/22/2013 - 20:00
Permalink
Git aliases and more
I use those aliases, too. I learned about them, and a lot more, from GitImmersion.com, which is one of the resources mentioned in the main lesson.
Mark
Wed, 05/08/2013 - 17:46
Permalink
Git project in a Git Drupal install?
Please add advice on how to download a project, if you have downloaded Drupal with Git. Is it OK to put a new Git repository for a project in a subdirectory of your Git Drupal repository? I've been advised it's not good practice--so what would be best practice here? Using drush to download and continually update the project?
Edit:
Drush doesn't look like a good option, as drush 5 doesn't work for D8 and drush 6 is still in beta. Would downloading a project with git , and adding the project's directory to
[drupal8-git-root]/info/excludebe the best route? Or create a .gitignore file in the root directory?benjifisher
Fri, 05/24/2013 - 10:20
Permalink
Git submodules
I only know enough to be careful about doing this. I think it is beyond the scope of this lesson, but you can probably get an answer by working through the examples at GitImmersion.com or one of the Git books listed on the Other Git resources page on d.o.
Whether or not you use Git submodules or .gitignore (or both or neither) depends on how you plan to use your repository. Is it just for development, or are you using Git to deploy it to your server?
The maintainers for Drush recommend using the latest version of drush 6 for daily work. They do. If you prefer something more stable, there is now a beta release of the 8.x-6.0 branch.