A basic Drupal development environment with Git

Tags: 

After many struggles, I've started working with Git on a real project. This post is about what I did.

Important: YMMV! There may be errors! No warranty is implied! The sky is falling!

This is what happened, to the best of my recollection. But I'm old, and I forget things. This procedure worked on my VPS, and my PC. Yours is different.

I forget things, too.

Git is a complicated, nasty beast, with fangs, and poison, and claws. It hates you. You, personally! With a passion.

I recommend you learn the concepts behind the Git commands. Spend a few hours. Your brain will get +2 defense against Git attacks.

Git workflow depends on task context, of course. My context:

  • My main project is Dolfinity. I am the only one doing development work on the project.
  • Dolfinity runs on a Hostgator VPS.
  • I do development work on a PC, using NetBeans. 
  • I don't know that I'm doing. With Git, that is.

My main goal is:

To be able to recover from mistakes.

There are many ways to mess up. Accidently delete files. Mix files from different versions of modules. Much can go wrong.

Some Events of Regret can be handled by backups. Some can not, at least not easily. For example, adding new features to code, removing stuff, replacing stuff, adding stuff, then finding out that I deleted a function that I need. Ack!

I admit it: I don't have a good workflow. At least, probably not. I've gotten to a Herbert Simonian intermediate stable state, but that's all. Don't do what I do. Maybe.

Computers and things

My setup:

  • An account on GitHub, with a private repository for the project. Actually, more than one, but let's keep it simple.
  • The GitHub repo is canonical, that is, the One True Set of Files. Not a running Drupal instance, just a file set.
  • A production version of the project on a VPS. It's a running Drupal instance. 
  • A version on my PC. It's a running Drupal instance, too. I work on it with NetBeans, a magical IDE with code completion, error checking for PHP, JS, HTML, and CSS (and more), and many other Goodies of Note for the Drupal Developer (GONDD). The XAMPP package supplies the local Apache and MySQL servers. 

This post is just about file version control. I haven't figured out the database side of things.

I want to be able to:

  • Change the code on my PC. Test, make it work. Push the changes to One True on GitHub. Pull the changes to the VPS.
  • Suppose I'm not at my PC, and notice something to change. I usually add it to a task list on the site. But what if I want to change it at once? I want to be able to SFTP or SSH to the VPS (three TLAs in 6 words!), and change a CSS rule or whatever. Then push the change to One True, and, later, pull the change to my dev PC.

There is another case: edit One True. It's possible with GitHub. But it sounds like blasphemy. 

Important thing: Some files are specific to each Drupal instance. One file in particular: sites/default/settings.php. I want to exclude that file from the Git push-you pull-me. (There are other files I might ignore, but let's ignore the ignoring of those files for now.)

What about interfaces? NetBeans - on my local PC - has GUI Gitty tools. I use them. I need procedures for that interface. 

Production, on the VPS, ain't got no steekin' GUI, gringa/o. It's command line. I need procedures for that interface. 

Setting up

One True

Create One True on GitHub. It's a private repo. The process is mostly pointy pointy, clickity clickity.

You tell Git to ignore a file, like settings.php, by adding its path to the file .getignore. I chose not to have GItHub make a .gitignore for me. GitHub's Drupal .gitignore template excludes more than I want.

One True exists, but is empty. All is darkness and void. Let there be files! 

Production (VPS)

This is where the canonical files live at the beginning of all this. So, I want to setup Git on the VPS, and push all the files to One True.

Steps:

  • Install Git. I don't remember how I did that. Typed in a Linux incantation. It was easy, I remember. Most nonegregious.
  • cd'd to the Drupal root, which is also the root of the site. Created the VPS repo, and excluded one file.
    (Is settings.php the Lucifer of One True, the Fallen File?)
  • Set up some configuration variables.
  • Link production (the VPS) to One True.

Notice that the VPS has its own private repo. The repo is independent of the One True repo. You synchronize them with push and pull commands.

Details:

Goal Command
Make the Drupal root your current directory. cd /var/..../httpdocs or whatever it is on your server.
Initialize an empty repo. git init

This will create a directory called .git, with a bunch o' bits in it. To see it listed: ls -al

Add all project files to the repo. git add -A

Git has a zillion commands, and a bazillion switches, like A. I only know a few of them. I copy commands from books, or pages on the Interwebs.

Exclude settings.php, by creating a .gitignore file. nano .gitignore

    (

Nano is an editor.)

Type sites/default/settings.php[ENTER]

Save the file.

You don't have to use Nano, of course, but I like it. An easy editor, with on-screen help. Easy is good.

Now you have a .gitignore file. To get a file list: ls -al

Set settings.

These commands will put data in the file  ~/.gitconfig. At least, it did on my VPS. The ~ (called a tilde) means your home directory. If you're root, ~ will be something like /root.

git config --global user.name "Darth Sauron"
git config --global user.email "sauron@fluffykittens.org"
git config --global core.editor "nano"

There are other things you can set as well. It's Git! There's always something else.

To check the settings:

git config -l --global

or

cat ~/.gitconfig

cat means "show on the screen." Really. More or less.

First commit.

git commit

The Nano editor will open, so you can type a commit message, describing the changes you made. Maybe: "Baby's first commit."

The commit command tells Git that you want to keep the changes you made to the files. Git creates a new snapshot in the local repo.

Link production to One True.

git remote add origin https://github.com/[user name]/[repo name].git

Danger, Will Robinson!

This stopped working. sad Changed to:

git remote add origin https://[GitHub user name]@github.com/
[GitHub user name]/[repo name].git

The VPS is gitted, and linked to One True. Now - drum roll, please - let's push to One True.  

git push origin master

Enter your GitHub user name and password. Then the files will fly. Look at 'em go!

Go back to GitHub. Look in One True. Files! There be files! Oh glorious day!

Dev PC

Start NetBeans. Make a PHP project. Not with remote sources, just a regular project. Kill the index.php file NetBeans creates for you. If you listen closely, you can hear it scream. Bwaahahaha!

Initialize a Git repo on the root of your project. The Git Init command is in the team menu, or something. Look it up. 

This is your third repo. It's independent of the other two. 

Now... magic time! In the project windowish thing, right-click on Source, then Git, Remote, Pull. The remote repo will be https://github.com/[user name]/[repo name].git. Enter your GitHub user name and password. Go! Then tell NetBeans to use the master branch. Go again!

NetBeans will suck the files from One True, down the Intertubes, onto your PC. Suck, Netbeans, suck!

Woohoo! Your NetBeans project is full of files. If you use beans, it will be full of beans. 

Workin' for the man

You're working away, changing this, changing that. "You haul 16 megabits and whaddaya get." Now you need to push your local changes to your remote repo. How do you Git 'er done?

Dev changes

There are two steps, maybe three. If you have created any new files, you need to add them to your local repo. In NetBeans, right-click on the files, then Git | Add.

Now commit the local changes to the local repo. Git | Commit. Enter a commit message.

The last step: Git | Remote | Push. NetBeans will show you the remote link you made before, and ask for your GitHub username and password. Tell it to use the master branch. That's it.

Suppose you change the code on your production server and push the changes to One True. You'll need to pull the changes to your dev PC.  Easy peasy. In NetBeans, Git | Pull. 

Production changes

You added some code at dev, and pushed it to One True. Time to update the production repo. 

Log in and cd to the site's home directory. Then type:

git pull

That's it.

What if you edit code on the server, and need to push to One True?

Optional: add new file:

git add -A

or

git add [path to file]

git commit

Then type a commit message in Nano.

git push origin master

No more

That's it. When I make Bad Mistakes, I've got a shot at Code Resurrection. 

Again, I don't know what I am doing with Git. I doubt I've set things up the best way, if there is a "best" way. If I can do it better, please let me know.

Still, this post might help someone understand how Git is part of a simple Drupal development environment.