When meeting new developers, one of the first things they ask me is whether I use Git during a theme or a plugin development. “Yes, of course, I couldn’t imagine my life without it”. A few years ago, though, the answer would be a puzzled look followed by a feeling of inferiority and shame. Their efforts at explanation proved futile. Looking back, understanding Git is not that difficult. So here’s my very high-level explanation. Or an attempt, at least.
Git in brief
“Git is a distributed revision control system with an emphasis on speed, data integrity, and support for distributed, non-linear workflows.”1 Whoa, not so fast my friend. The preceding quotation, translated into a simpler language means that:
- Git is a tool used to store changes to files in a project (revision control),
- Git allows many programmers to work on the same project from their own computers wherever they are (distributed),
- Git is speedy and won’t corrupt or break the files,
- and that the programmers can edit the same file(s) without nothing bad happening.
Git is a standard application which you install on your computer. You then operate it by typing and executing some commands in a command line. Don’t know or don’t want to use the command line? Not a problem — Git can be used from within a GUI application2, too.
Using Git for themes and plugins development
I’ve read that we, the people of Earth, learn the best by examining examples. Especially me. Let’s imagine that we are going to create a WordPress theme. The first step is to persuade you to use Git. Then, I’ll show you how to actually work with it.
Imagine being able to go back in the history of your theme. Inspect every change.
Many developers working on the same theme? With Git, each modification — even adding a new line — is attributed to a particular developer, therefore allowing us to see who does what and when. What if the developers are working on the same file? Git undertakes an intelligent merge3 utilizing various strategies so no one’s code is lost or overwritten. In a case it doesn’t know how to perform the merge, it will politely prompt to do it ourselves.
Another substantial benefit of using Git during building our themes is that by hosting the Git repository (the
.git folder, more on that later) on an external service such as GitHub or Bitbucket, our source code is basically backed up. Then, if we have a disk failure or lose our computers, we simply clone4 the repository and continue working where we left off.
Now that we acknowledge how awesome Git is, let’s go through a brief tutorial on its usage.
Download and install Git5. Within the command line, navigate to your WordPress theme’s directory. While the steps would be a bit different if we worked with a Git GUI, the ideas would be the same, so try to follow them. In the theme’s folder, type and execute this command:
git init initializes a new Git repository in the form of a hidden
.git folder — a place where all the file changes are stored.
For a theme to work properly, we need two files:
index.php. Create these files and add some code to them. Let’s say that we want to store these changes so we can get back to them later on. It’s a two-steps process:
- Add the changed files into the Git’s staging area6
git add --all
- and commit (save) the staged files so they become permanent.7
git commit -m "Add style.css and index.php (initialize the theme)"
-m. When we commit some changes, it’s a great idea to provide a short description of what we did. That’s what commit messages are good for.8
Do you want to see the commit history? Execute
git log. Here’s how it looks like:
I have a modified command line graphics9 but the information is basically the same.
Repeat the above steps a few more times (modify files, add them to staging area and do a commit with a clear message) and see the Git commit history. Nice, isn’t it?
The Internet is full of Git tutorials written by brilliant people. I learned a lot from this visual “game” about Git commits, branches and more10.
I’ve described Git as a useful but quite simple tool. However, as it was “initially designed and developed by Linus Torvalds for Linux kernel development in 2005”1, Git contains plenty of advanced features under the hood. Namely:
- branching: “If there is more than one dev working on a code base then you should both be working on your own branches. The master should contain the current revision of the code with nothing extra.”
- remoting: pushing and pulling data to and from remote Git repositories
- cherry-picking: “grab just a specific changeset and apply it to your history; no need to merge the entire branch”
- stashing: “Not ready to commit yet but need to change gears and make a quick change for someone else? Save your working copy in a temporary place, and then use
git stash popto get it back.”
- resetting: “reset the Git repository to previous revision”
- and much more…
How does GitHub relate to Git?
The fundamental difference is that while Git is a tool (application), GitHub.com is a website. GitHub is a service for hosting our Git repositories, much like DigitalOcean is a service for hosting our websites. Although there are other online services for Git hosting (Bitbucket, etc12), GitHub is the most popular one. What is more, for open source projects, GitHub is free!
Despite trying to explain Git in as much detail as possible, it is a large topic for a single post. That’s why I’m giving you some external resources you might find useful.
- Git on Wikipedia ↩ ↩
- Git GUIs ↩
- Basic Branching and Merging on Git SCM ↩
- Git clone ↩
- Git Downloads ↩
- Git staging area ↩
- Recording Changes to the Repository ↩
- Writing good commit messages ↩
- Lamosty’s dotfiles ↩
- Learn Git Branching visual “game” ↩
- When and how to use the advanced features of git? ↩
- 12 Git Hosting Services Compared ↩