How to start using Git on an existing Drupal project

| | 5 min read

Git is one of the most versatile opensource version control systems out there. If you have not already started using Git for your Drupal Development projects, this article should help you get started with it.

Please note: I have been using Linux as my development environment, and almost all my illustrations would be for Linux. Still, the commands for using git should be the same for git implementations on all OSes. Please add in a comment if you have any doubts.


  1. Download all the files of the existing site.
  2. Run the command git init, from the webroot directory. This command creates an empty git repository or re-initializes an existing git repository. When this command is executed, a hidden directory named ".git" is created inside the webroot.
  3. Add a .gitignore file inside the webroot. (This file contains the name of files and directories which the user does not want to track via git).
  4. Add all the files in the webroot to git. This is done using git add command. It adds all files in the directory. If you want to add each file one by one, you can specify the file path instead of the dot(.) symbol.
    If any files or directories are specified in the .gitignore file, they will be ignored. If you try to add a file that is added to the .gitignore file, you will get the following message:
    The following paths are ignored by one of your .gitignore files: 
    ...list of file paths...
    Use -f if you really want to add them. 
    fatal: no files added 
  5. Once all the files are added to git, the details should be logged into the repository. This is done using the command git commit. You can add a message to describe about the changes made in the current commit


Here I demonstrate how I normally add an existing project into git:

I've a Drupal 7 installation in my system, which I wish to add to a git repo. It is in a folder named 'drupal7' and placed inside /var/www/. So my webroot is /var/www/drupal7.

From the terminal, I move to the webroot of my project 'drupal7'.

[email protected]:~$ cd /var/www/drupal7/

I'm going to list all the files in my webroot using 'ls' command

[email protected]:/var/www/drupal7$ ls -a

.              .gitignore         install.php         modules     sites
..             .htaccess          INSTALL.sqlite.txt  nbproject   themes
authorize.php  includes           INSTALL.txt         profiles    update.php
CHANGELOG.txt  index.php          LICENSE.txt         README.txt  UPGRADE.txt
COPYRIGHT.txt  INSTALL.mysql.txt  MAINTAINERS.txt     robots.txt  web.config
cron.php       INSTALL.pgsql.txt  misc                scripts     xmlrpc.php

There is a .gitignore file inside the drupal7 webroot. The content of that file is as follows:

# Ignore configuration files that may contain sensitive information.

# Ignore paths that contain user-generated content.

We add files or directories which we would like to be ignored by the git repo to this list.

Now we initialize git in this fodler using git init command. I also run the ls command to list the contents of .git directory that was created when the git init command was executed. I've also executed the git status command. It shows the branch I'm currently working on and the list of untracked files in the directory.

[email protected]:/var/www/drupal7$ git init
Initialized empty Git repository in /var/www/drupal7/.git/
[email protected]:/var/www/drupal7$ ls -a .git
.  ..  branches  config  description  HEAD  hooks  info  objects  refs
[email protected]:/var/www/drupal7$ git status
# On branch master
# Initial commit
# Untracked files:
#   (use "git add ..." to include in what will be committed)
#	.gitignore
#	.htaccess
#	INSTALL.mysql.txt
#	INSTALL.pgsql.txt
#	INSTALL.sqlite.txt
#	README.txt
#	authorize.php
#	cron.php
#	includes/
#	index.php
#	install.php
#	misc/
#	modules/
#	nbproject/
#	profiles/
#	robots.txt
#	scripts/
#	sites/
#	themes/
#	update.php
#	web.config
#	xmlrpc.php
nothing added to commit but untracked files present (use "git add" to track)

I want to ignore .htaccess file from git repo. So I add that file into the list in the .gitignore file. If I again execute 'git status' command, .htaccess file will not be listed below the untracked files.
Now I've to add all the files under the webroot to git. For that I use the command 'git add .'. The files/directories listed in the .gitignore file will be ignored. If you execute 'git status' command now, you can see that all the files/directories which where listed under 'untracked files' will now be under 'Changes to be committed:'.
Now you have to register all the additions and changes to git repo using 'git commit' command.

[email protected]:/var/www/drupal7$ git commit -m "Initial commit"

Now all the files will be added to git repo. I'm going to execute a few more commands, for checking.

[email protected]:/var/www/drupal7$ git status
# On branch master
nothing to commit (working directory clean)
[email protected]:/var/www/drupal7$ git log
commit 217d74d5cf4af00e2d634893a844b5848aa5b27c
Author: smruthy 
Date:   Mon Jul 2 12:50:28 2012 +0530

    Initial commit
[email protected]:/var/www/drupal7$ git branch
* master
[email protected]:/var/www/drupal7$

'git status' shows the branch you are currently working on. Since there are no changes done after the last commit or any untracked file, it is showing the message 'nothing to commit (working directory clean)'

'git log' displays the details of the commits we have done to the git repo; with commit id, commit message, date and author who did the commit.

'git branch'. lists all the the branches in the repo. Now we have only one branch 'master'. If there are more branches, they will all be listed and the current working branch will be marked with *.