Install Git

Ready for review -- Last peer review: 9 Mar 2013
Description: 
Install Git on your personal computer
Overview: 

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:

  • Mac OS X
  • Windows
  • Linux
Resources:
Steps: 

Note: instructions on command line tools commonly include the prompt, usually a dollar sign ($). Do not type the prompt when you are following the instructions; you only want what follows the prompt.

Installing Git on Windows and Mac

  1. Download the latest version of Git for your operating system by going to http://git-scm.com/downloads.
  2. Run the installer. For Windows, you can accept the defaults as they are all reasonable. On the "Adjusting your PATH environment" screen choose "Run Git from the Windows command prompt.")
  3. To confirm Git has installed correctly, open a terminal window and at the prompt, type 'git' (without the quotes). A list of the most common Git commands will print to the screen.

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.

  • pwd (present working directory)
  • ls (list) for information
  • cd (change directory). For example, if you are already in a Drupal root directory, type the following to move to your modules and then themes directories (using .. before a path moves up one directory):
    $ cd modules
    $ cd ../themes
  • CTRL + C will cancel an action.

Basic 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.

  1. Tell Git who you are. Use whatever name you would like to have publicly associated with your commits. Note that if your name has a space in it, you have to put the quotes around it.
    $ git config --global user.name "Your Name"
  2. Provide your email address where anyone with questions about your commits can contact you.
    $ git config --global user.email yourname@example.com
  3. Set Git to use safe line endings.
    $ git config --global core.safecrlf true

Download 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.

  1. Move into the correct directory for your project, e.g. cd modules from your Drupal root.
  2. Find a project on Drupal.org (for example, Admin Menu). On the project page, click the "Version control" tab (near the top of the page).
  3. Select a version of Drupal from the drop-down list that matches your Drupal site version.
  4. Copy the git clone and cd commands listed there. Paste them into your terminal window. (If you are using Git Bash on Windows, you can click the icon on the upper left corner of the application window to get a menu, where you can select Paste, or you can use the shift+insert keyboard shortcut.)
  5. Once the clone is complete, try these informational commands:
    $ ls -a
    $ git status
    (view the status of your files in the working directory and staging area)
    $ git branch -a
    (list, create and manage working contexts. The option -a shows both existing and remote tracking branches)
    $ git log -3
    (shows commit logs. The -3 option limits to the latest 3 commits.)

Download 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

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.

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's picture

Under "Installing Git on Windows and Mac" it'd be useful to provide Homebrew instructions:

If you use Homebrew on OS X:


$ brew install git bash-completion

Update status: 
Ready for review
sebi's picture

Here are some useful aliases to setup with git.

git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch

Then you can simply call git br to display the branch, etc.

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.

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/exclude be the best route? Or create a .gitignore file in the root directory?

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.