An introduction to Git (part 5): Using Git with Drupal and Git hosting services
Share with Others
In An Introduction to Git Part 4, you learned the basics of Git branches. You are now ready to push your Git repository to a Git hosting service such as Github or Bitbucket. After that, we will bring it all together and discuss possible ways to use Git in your next Drupal project.
Git is one of the secrets from my 5 secrets to becoming a Drupal 7 Ninja ebook, and much of the following posts are from this ebook. To learn more about Git and the other secrets, please consider purchasing the ebook or signing up for the 5 secrets email newsletter which will give you more information about the 5 secrets.
Pushing your Git Repository to Github or Bitbucket
In the next few sections we will look at both Github and Bitbucket and how to push your Git repository up to these Git hosting services. This section will only prove to be a really quick getting started. There is ample documentation on both of these services online if you choose to pursue using one or the other. First we will look at Github.
You will first need to create an account on github.com or bitbucket.com. Once you are logged into your account, you will want to set up your SSH keys. Your SSH Key is how Github or Bitbucket knows who you are. This way when you push up your changes, Github or Bitbucket will be able to determine if you have access to the Git repository you are trying to push or pull from. The first step is to make sure you have an SSH key setup on your local system. Since the setup varies depending on your operating system, you will want to search for something like “Generate SSH Key [operating-system]”. This should give you some results. If you are using a Linux based operating system or a Mac, you can probably just run the ssh-keygen command (however you should probably do a little research so you can understand the various options).
After you have an SSH key setup on your local system, you will have a .pub file available to you in your ~/.ssh/ directory (this might be slightly different on a Windows computer). Open up the .pub file (in my case it is id_rsa.pub) and copy the contents of the file.
In your Github or Bitbucket account, you will need to find the place in your account settings to manage your SSH Keys. You will need to add your SSH Key and probably give the key a title. You will then need to paste the contents from your .pub file into the Key section. You should now be hooked up to your Github or Bitbucket account allowing you to push and pull your Git repositories.
The next step is to create a new Git repository. When you create a new repository on Bitbucket or Github, you will be walked through the basic setup steps. You will then be given a few options and Git commands you can run. You should see some instructions showing how to push an existing repository from the command line.
Adding a Git remote
The first step to push up your existing Git repository is to add a new Git remote. You will then use this remote to push your Git repository to your Git hosting service.
|Git Command||What does it do?|
|git remote add [remote-name] [remote-url]||Adds a remote repository called [remote-name] located at [remote-url]. We can now push and pull from this repository (assuming we have permission).|
|git push -u origin master||Push our master Git branch to the remote called origin.|
You should be able to copy and paste the git remote add command from Github or Bitbucket into your command line and run it. Then do the same with the git push -u origin master command. Your output should look something like this.
If you refresh the Github or Bitbucket page, you should now see your repository listed. You can now look through and begin using the online Git tools on your Git repository. Anytime you make a Git commit to your master branch you can simple run the git push origin master command to get the changes to be pushed up to Git hosting service. If someone else pushes changes to your Git repository and pushed it up, you can run a git pull origin master to pull in the changes to your local Git master branch.
You may have noticed that only the master branch is listed on Github or Bitbucket, you can push your other branches up as well. Simply run the git push origin [branch-name] command to push up a branch to your Github or Bitbucket repository or run git push origin --all to push up all your branches.
Using Git with Drupal
Now that you know the basics of Git, you are ready to apply this to your Drupal website. Go to your document root of your Drupal website and run the git init command.
Drupal 7 comes with a .gitignore file for us to use. If you are using Git for a non Drupal project, you could create this with any text editor. The contents of the .gitignore file exclude the settings.php file, the Drupal files directory, and the Drupal private files directory. Here are the contents of the .gitignore file.
These patterns are set up to match files such as sites/default/settings.php as well as files such as sites/test.com/settings.php. This allows the Drupal multisite functionality to work correctly without allowing you to accidentally add in user generated files or a settings.php from one of your Drupal websites.
You are now ready to add all the files to your Git repository and make your first commit.
You are now able to use the Github, Bitbucket, or any other Git hosting service to push your repository up using the instructions above.
You may be thinking... "Is that it?" And the short answer is No, but it's a good start. There is not much difference using Git with Drupal or using Git on other types of files. The important thing is to remember that user generated files should not be added into the repository, and that you will most likely not want the settings.php file added into the repository (Note: some hosting services add in the settings.php file by loading in database credentials and other environment specific information from another file... but the important part is that your database credentials should not be in your repository).
Git summary: Where do you go from here?
Learning Git is intimidating. There are a lot of moving parts and a lot of things that can seem to go wrong. You will run into problems such as merge conflicts or trying to figure out how to undo commits, but these hurdles are worth the effort. Everyone makes mistakes, but what separates a Drupal developer from a Drupal Ninja, is the Drupal Ninja knows how to quickly and painlessly recover from those mistakes. You probably use some type of backup system for the hard drive of your computer. Git is like a supercharged collaborative backup and recovery system for your entire project.
If you liked learning about Git, make sure to check out the 5 Secrets to Becoming a Drupal 7 Ninja. It has some additional information on Git and other helpful secrets for building awesome Drupal websites. Also, grab a copy of the Code Karate Git Cheatsheet, to make remembering Git commands easy when you are just getting started.