Skip to content

Republic / Articles /

Article updated 2025-02-05.
Written by Christofer Sandin.

This is an older article, so some references and links may be outdated. Keep that in mind while reading. Thank you!

Version Control with Git

We had been using Subversion for quite a long time, and it worked very well, but since all the cool kids are using distributed version control systems like Git or Mercurial, I thought we should take a look at how to get started with Git.

This is not a complete guide, but rather a cheat sheet with the most common commands and concepts. I assume you can install Git on your own and that you have some familiarity with using the terminal. The article was primarily written for our own benefit while we transitioned to Git, but if someone else gets a bit of help from it at the same time, that’s obviously just great.

A Better Environment

A major advantage of Git is that it is relatively easy to create different branches in a project. For example, it can be useful to have a branch where you develop a new feature while you have another branch where you only fix bugs in the meantime. When you are done with the new feature, you can merge these two branches, through a so-called merge, thus incorporating both the bug fixes and the new feature.

A More Informative Prompt

A good start is therefore to ensure you can easily keep track of which branch you are currently working on. I think Devot:ee has a good description of how to get a more informative prompt a bit further down in their article about Git.

NetTuts also has a good article if you want to customize your prompt.

Settings

If you, like me, are on a Mac and working in TextMate, it's best to ensure that TextMate is your default editor:

git config --global core.editor "mate -w"

If you are working in Sublime Text, the following applies:

git config --global core.editor "subl -n -w"

Also take the opportunity to add your name and email address:

git config --global user.name "Your Name"
git config --global user.email "your@email.com"

Create a Local Repository

It is perfectly fine to use Git locally on your computer when developing your own projects. Even though Git really shines in some form of network where multiple people or at least different computers are involved, a local repository is a great way to learn.

Create a New Project

In the terminal, change to the directory where you want to create your project and run:

git init

Now the project is initialized, and a directory named .git is created. Unlike Subversion, which creates a .svn directory in each project directory, there is only one .git directory in the root directory of the project. If you want to remove version control, you can simply delete this hidden directory.

Ignore Certain Files in the Project with .gitignore

Create a file named .gitignore in the root directory and add all the files you want to exclude in this. For an ExpressionEngine project, it might look like this:

# cache files
.DS_Store
admin/expressionengine/cache/**/*

# config files
admin/expressionengine/config/config.php
admin/expressionengine/config/database.php

# uploaded images
images/avatars/uploads
images/captchas
images/member_photos
images/pm_attachments
images/signature_attachments
images/uploads

Keep Track with Status

To see which files have been added, removed, and edited since the last commit, use the status command:

git status

Adding Files to Git's Index with Add Before Commit

Once you have worked on your files, it's time to add them to Git's so-called index before you can make a commit. This is an extra step compared to Subversion, but it is quite practical as you can easily choose which files should be included in the same commit.

To add all files, just put a dot after add:

git add .

Or if you want to be more specific, you can certainly do that:

git add .gitignore
git add *.php

To also include files that you have deleted in the project, you can use the -u flag; otherwise, these files will show up every time you run a status:

git add -u

To remove one or more files that you have added, use:

git reset HEAD file1 file2 …

Then, Time for a First Commit

When the files you want to add are added with add, it's time to make a commit. This is done by:

git commit -m "This is a commit message"

A new check with status shows that there are now no reported changes since the last commit. Here’s an example of this:

~/Sites/project (master): git status
# On branch master
nothing to commit (working directory clean)
~/Sites/project (master):

Create and Merge Branches

As I mentioned at the beginning, one of the most appreciated features of Git is the ability to easily create and merge different branches of the code. Here are the basic commands. They are usually named

Create a Branch

To create a new branch, you run:

git branch branchname

To switch to another branch, use:

git checkout branchname

This will also change all files in your working directory, giving you a representation of the current branch's files. To see what different branches exist and which one you are currently working on, just type:

git branch

Also, note that the current branch is always included in your prompt:

~/Sites/project (master): git checkout branchname
~/Sites/project (branchname): git checkout master
~/Sites/project (master):

Merge

To then merge a branch with the current branch you are working on, use the merge command like this:

~/Sites/project (master): git merge branchname

This would merge the branch named branchname with the current branch master.

And Finally, Delete a Branch That Is No Longer Used

~/Sites/project (master): git branch -d branchname

Push a Repository to a Server That Everyone in the Project Can Access

We use an external server for some of our projects. Services like Beanstalk and Springloops make it easy for those who don’t like the command line to log into the web interface and see what’s happening.

To push the repository you created locally, first add a new project in Beanstalk.

Locally, then add Beanstalk's server to your git project:

git remote add beanstalk git@republicfactory.beanstalkapp.com:/project.git

This adds the following lines to your .git/config file:

[remote "beanstalk"]
url = git@republicfactory.beanstalkapp.com:/project.git
fetch = +refs/heads/*:refs/remotes/beanstalk/*

Now you can use beanstalk instead of git@republicfactory.beanstalkapp.com:/project.git.

When you have made changes locally, run add and commit, you can now run push to add your changes to your repository at Beanstalk. This works exactly the same way with any other server.

git push beanstalk master

Clone a Repository to Another Server

If you then have a development server accessible via SSH, just log in there, create a new directory, and clone the project from Beanstalk. This can be compared to "checking out" a repository with Git:

git clone git@republicfactory.beanstalkapp.com:/project.git .

The dot at the end makes it so that you create the .git directory in the directory you are in instead of the repository you check out ending up in its own directory. To keep the development server updated with the changes that have been committed to Beanstalk, just run:

git pull

You can of course also clone a repository on your local computer and keep this updated through git pull.

References

I have not used Git for a long time, and this little cheat sheet will be updated now and then. It is by no means everything you can do, but it at least provides a small introduction. Below is a list of interesting articles if you want to read more about Git.