Tips & tricks for Drupal and GIT

Tips & tricks for Drupal and GIT

Ever since Drupal 7 I've used GIT to keep track of both my personal repos as the ones the company I work for manage. In short, I use GIT quite a lot. A colleague of mine use GIT to keep track of his computer setup so he easily can pull down his settings and .profile whenever he chooses to reinstall his computer or get a new one. I've heard of a guy (or girl, can't remember) who use GIT to keep track of all his documents. That seems a bit handful, but GIT is wonderful to keep track of the changes in your, for example, Drupal repo.

And even if I've used GIT for so long, I'm still learning new stuff about GIT and it makes my daily work even better and more streamlined. Below I'll share some of my discoveries when working with Drupal and GIT.

I've updated many modules, but I want to commit them one by one

I've done a total update perhaps, or anyway updated more than one module, but I want to make separate commits due to OCD or company guidelines, well as long as the module is located in it's own folder (which they are), that's no problem.

Example: I've updated two modules, Token and Metatag, and now I have a list of 40+ files which are listed as either modified, deleted or new. Instead of doing a total commit of them all, I simply add the folders, one by one, and do separate commits of them.

$ git add sites/modules/token
$ git commit -m "Updated Token module"
$ git add sites/modules/metatag
$ git commit -m "Updated Metatag module"

Two modules, nicely committed one by one. Now you can sleep without any OCD nightmares.

Add all files, but not all!

This is one of the latest I learned. Sometimes, when I update Drupal Core I want to add all files, but for some reason I want to exclude one or two files.

Example: I've updated Drupal core, which led to 60+ modified, deleted or new files but among them I have my settings.php and an updated module (let's choose Metatag again) that I want to exclude. I could do a separate git add for the module folder if I want, but perhaps I don't want to commit it at all, and that's where this comes in handy.

$ git add -A    // Add all the files
$ git reset -- sites/all/modules/metatag**     // "un-commits" the metatag module folder
$ git reset -- sites/default/settings.php    // "un-commits" the settings.php
$ git commit -m "Updated Drupal Core like a pro!"

I've made changes to a file, but want the original file back

This happens a lot. You try something out, go wild, and then suddenly want your original file back. No worries. Just use this to make the changes disappear and the original file will magically appear again.

git checkout [filename]

You can even make all the changes disappear, going back to square 1 on your repo.

git reset --hard

That command will not erase new files, though, so you might be standing with a bunch of new files that GIT won't ignore. You can either delete them, file by file, or you can use the following command to erase all the un-committed files:

git clean -d -x -f

There's changes in my file? What changes?

Sometimes when you upgrade a module or if you just come back to a project and you can't remember what you've done, there's a quick and easy way to see the differences in a file. If you do a git status and a file comes up as changed you can type git diff followed by the filename or complete path to the filename and you'll see the changes. If there are a lot of changes you might want to check out other ways of analyzing the changes. If you're using Sublime Text Editor I can recommend Git Gutter.

$ git status    // Lists all changed files
$ git diff .htaccess   // Shows the changes in the .htaccess-file

That's a few examples of what you can do with GIT, use it for. As I discover more, I'll write them down here so we can spread the knowledge.