Categories
Git WordPress

Git for WordPress – Getting Started

logo-git

Motivation

We have been trying to get Git implemented into our theme development process for a rather long time. This didn’t work out, though, since Git is a bit difficult to grab if you don’t feel at home in the abstract vicinity of the command line.
So we set out to work this out. Me, more the developer and geeky type on one side and Kirsten, my partner, designer and maker of custom WordPress themes on the other side.
We finally managed and wanted to share our experience with anybody who might be getting more and more frustrated about “just not getting Git”.


This article is part of a series.
These titles are scheduled so far but might be subject to change along the way:
1. Git for WordPress – The Beginning
2. Git for WordPress – Oh no, the command line!
3. Git for WordPress – Some real important Git commands
4. Git  for WordPress – Working with Branches
5. Git  for WordPress – Projects on GitHub
6. Git  for WordPress – Merging your versions
7. Git  for WordPress – Tools and Tips


Some History of Git

To understand some of the peculiarities of Git it might help to know a bit about it’s history. If you understand what the intention was in creating it you might just understand some things better.
Git was actually developed by Linus Torvalds when the Linux project needed a more efficient version control and source code management system.
Considering a complex project like that, the main needs Git needed to fill were:

  • Many developers had to be able to work on it independently and
  • you had to be able to put all the pieces together as easily as possible.
  • It needed to be fast and efficient.

Advantages of Git

So if it is so complicated to learn, why bother? Isn’t there anything out there that’s easier to work with?
Git does have a couple of advantages we learn to enjoy once we got started.
It is a free distributed version control software. This means, once you got a copy (“fork”) of a project you have the complete history of the project, all the information that’s available.
With that you are able to work on it on your local machine and don’t need a connection to the internet or anything like it.
For people developing WordPress themes there are some real interesting projects on GitHub. Sure you can just copy them from GitHub and work with that copy. But if you do this using Git you can also get updates and bug fixes from the original project and update your local copy easily.

What does version control the Git way mean?

Version control means that you are able to access older versions of your project easily or even role your project back to a certain point if necessary.
Git is said to be especially fast and lean. That’s due to the way Git keeps track of your changes:
Git works with what it calls “snapshots” of your project. That means it conserves the state of your project at the exact moment when you requested it to take a snapshot. (In Git language this translates to a “commit”.)
Such a snapshot consists of the changes you made, not of complete files saved over and over again: Files that didn’t change are only referenced to the latest version.
Looking at your snapshots you can access all the changes that were made throughout the development of the project. Git offers a vast amount of tools and methods to display and compare different versions of your project or single files.
That probably is part of why it can be so confusing in the beginning.

Some terms you will encounter learning Git

commit

commit means that you ask Git to take a snapshot of your project the way it is right that moment. This way this state of your project is kept in your history for you to get back to if necessary.

branch

A main feature of Git is that it allows you to create branches of your project. You can picture it like the branches of a tree. You can use a branch to try new things or develop a certain feature of your project without affecting the stable version of your project.

master

Branch master is created automatically as soon as you initiate Git in your project directory.

Workflow with branches

Let’s assume you are developing a WordPress theme and want to work on a slider for your homepage. We want to work on this without affecting the stable version of our theme.
Being in master branch, we can tell Git to create a branch called “slider” and change to this new branch. From this moment, everything we do will be recorded as belonging to branch “slider”. Once you do a commit it also belongs to branch slider.

As soon as you go back to branch master you won’t be able to see your work on the slider anymore.
If you now have an idea concerning the footer you might want to create a new branch called “footer” and work on your footer there. This won’t influence either your master or your slider branch.
In order to get back to working on the slider you need to change back into your slider branch.

Does your slider work nicely now? Do you want to add it to your stable version in your master branch? Then you only need to go back to your master branch and merge it with the slider branch.
Have a look at some examples for different workflows at altassian.com.

Managing merging conflicts

When you are merging different versions of your files, you are bound to encounter some merging conflicts. Most of the time Git works out merging conflicts all by itself. Only if the exact same line of code has been changed in both versions it needs your help.
In this case you are able to look at the different versions and decide which one you want to keep. Or you can build a completely new version, using the best parts from both branches.

Infographic Git Basics

We developed an infographic showing the basic Git workflow we talked about before. Kirsten added some translations from “Git Greek” to English.

Minimal Git workflow