Intro To Source Control

share this page

If you’re serious about game development, you’ll save yourself a lot of time and headaches by learning about a source control.

In this lesson we’ll learn to make a git repo with github, commit changes, and push those changes to a remote server. It’s time to GIT GUD! (get it? Yeah ok, I’ll stop)

Learning goals

  • Create and use a repository
  • Make changes to a file and push them to GitHub as commits
  • What is source control?

    Source control is a way to backup and organize your code so that if anything happens to your computer you’ll always have a backup copy and won’t lose hours, or worse… months… of work. Sure you don’t need to use source control to make games, but I highly suggest you do and I strongly encourage anyone interested in game dev to make it a regular part of your development process.
    In this tutorial, we'll be using git to store and backup our files. If you don't want use git, at the very least use a service like dropbox to back up your files. However, be warned that in a game development studio you'll probably be required to use some sort of source control, so it's a good idea to learn how to use it properly. In this first tutorial, we'll be using the github desktop client, but in the future we'll also learn to use git with your computer's command line.

    Getting started

    Before we can start our lesson you'll need to open an account with a company that allows you to create a git depot. Both github and bitbucket allow you to have free git repos. On github these repos will be public (everyone can see your code) while bitbucket allows you to make public and private repos. For this lesson, we'll focus on github, but most of the things that we'll do in this tutorial should be transferable so choose the option that works for you.Sign up for a free github account here or Sign up for a free bitbucket account here.

    Making a repository

    Once you have an account, make a new repository (repo) by logging into your github account and clicking on the "+" menu. We'll use this repo to play a version of the game Exquisite Corpse in another post, so select "new repository" and name your repository "ExquisiteCorpse". Finally, click "Initialize this repository with a README".

    Setting up the desktop client

    Now that you created your new repo, it's time to add something to it. We'll be creating and adding the files "story.txt" and "nextLine.text." To do this first download the github desktop client. Once you finish downloading the desktop client, sign in, and configure the client. During this process, you should see the following screens:

    Cloning the repo

    Next we need to get the repo you made on the git hub website onto your computer. We'll do this by cloning your repo.

    With the git desktop client open got to File->Clone Repository. You should then see a pop up window asking you for the repo Url or github username and repository. You'll also be asked for your your Local Path. For the repo Url, go back to your github repo page and click on the Clone or download button and copy the Url.

    Your Local Path refers to the location on your computer where your repo files will live. Click on the choose button and navigate to the folder you want your files to live in. You should now see a new folder titled "ExquisiteCorpse" in the folder you chose. Open the folder and you should see a file called README.md. README.md is a text file that includes any and all information about your repo. This might include things like credits, license info, instructions on how to run your code, or patch notes. For now, open it up with a plain-text editor. If you don't have a plain text editor I recommend downloading Notepad++ for windows users and textmate for mac.



    Adding files and committing changes

    When you open README.md for the first time there should only be one line of text "# ExquisiteCorpse". Let's add a line to the file that describes this repo. Something along the lines of "This is a version of Exquisite Corpse to learn how to use git."

    Next, let's add some files to the repository. Using your plain-text editor, make two new files called "story.text" and "nextLine.txt" and put them in the same folder as your "README.md" file.

    Finally, go back to the github desktop app. it should look like the following screenshot.

    There are quite a few things happening in this screenshot so let's break it down a bit.

    First, you'll notice that all of your files are listed on the left side of the app under changes. The colors next to the file names tell you a few things about those files. Green shows that new file that has been added to the repo, Yellow shows that a file has been modified, and Red (which we don't see right now) would indicate that the file has been removed.

    On the right, the git desktop app will show you what changes have been made to your files with line numbers indicating where the changes took place. This section of the app follows the same Green, Yellow, Red color scheme as the file tab.

    Now that we've added files, let's commit them. Fill out the summary field with a message that accurately reflects the changed you made. In this case, the message can simply be "Added initial files" or initial commit. However, as the project evolves you'll want to make this message more descriptive, especially if you're working with others. Once you're done click on the "Commit to master" button. Congratulations! You just made your first commit! Now... what does that mean? Well, it means that your computer is now keeping track of the changes you make to your files. In order to fully understand what that means let's do something drastic. Let's delete one of the files we created.

    Tracking changes

    Go back to the folder with the files we created. Select the README.md file and delete it. Next, open your github desktop app and you should see something like this:

    You'll notice that the github app knows you deleted the file. Let's pretend that you deleted this by accident. Normally this would be a very bad thing. Fortunately, github can help us out. Go back to the github app and right click on the changes tab. You should be given the choice to discard all changes. Click this option and go back to the file containing our files. Surprise! The file is back to the same sate it was when you made your frist commit.

    Pushing your files to github

    Now that you're tracking your changes locally (on your computer) we'll talk about another powerful feature of source control: backing up your files on a remote server. Go back to the github app and click on the button that says Push Origin. Clicking this button publishes all of the changes to your github account. To see what this means, go back to github.com, log in, and navigate to your ExquisiteCorpse repo. You should now see your files on your account.

    Now... why would you want to do this? Pushing your changes to your github account not only backs up your files, but also allows you to access them from any computer. Lets say your computer breaks down and you have to get a new one. As long as you pushed your commits to github you'll be able to start right where you left off just by re-cloning your repository (check the cloning section for a quick reminder about how to do this).

    Congratulations! You have now created a repo, learned to tracked changes locally, and published those changes to your github account! Supercharge your gamedev with part 2 of our github tutorial!

    Got a git hack you’d like to share? Contact us, connect with us on Facebook, or let us know on Twitter.

    Till next time, game on!