Keep the change: use Git with Unity

All of us, at some point in life, experience the grief of loosing work that is done. If it didn’t happen to you, you’re in the lucky few! But trust me, it’s one of the worst experiences for a programmer.
There are many ways you can protect your work with Unity, and I’ve tried a lot: the Asset Server, Dropbox, manual backups, you name it.
Out of all, Git is the one to rule them all. It’s extremely easy to use, MonoDevelop integrates with it wonderfully, and you can safely save all your code and your binary files!
Here are the steps and the tools we use to keep our work safe and versioned with Git and Unity.
I’m going to show you how to use BitBucket, so the first thing you need to do is to create an account there. Follow their instructions to do so.
Then, download their very own SourceTree app, which is a very simple, lightweight and convenient IDE for Git. Follow their instructions to download the app and log into your account.
Let’s start by creating the Git repository. On the SourceTree app select “+New Repository” and then select “Create Local Repository”
Choose a destination path of your liking, and a name for the repository. Make sure Git is selected on Type, and the “Also create remote repository” is checked:
After that, you’ll get the remove repository creation window. Again, make sure the Type is Git and the “This is a private repository” check needs to be checked. Otherwise your code will be public!
You should end up with this:
Now it’s time to jump to Unity: let’s start with an empty project. Make sure you select the folder for your local repository in Unity’s new project window:
Once your Unity empty project is created, you’ll notice on SourceTree there’s a number in black next to the “master” text on your repository’s row. That means SourceTree detected new files and it’s letting you know you are “ahead” the master branch. That means: you have changes that are not yet uploaded to BitBucket. Go ahead and double click the “MyFirstGit” row on SourceTree. You will get something like this:
At this point we will be saving absolutely everything to Git, but there are plenty of files we don’t need saved, such as temporary files. For that, Git offers what is known as a .gitignore file, which is basically a file in which we specify the files and folders we want to ignore.
To access it, go to Repository menu -> Then Repository Settings -> Advanced Tab and you’ll see a Repository-specific ignore list, and an Edit button. Click that button and type the following:

Save and close the file, hit “Ok”.
We now need to add our remote repository to this SourceTree local one. Go again to Repository Settings, but this time to the Remotes tab. Click the “Add” button and then click the world button to the right of “URL / path” textfield. You’ll be presented with a list of repositories you have.
Select the one you created before. In our case, “MyFirstGit”. Enter a name as you wish. You can put “MyFirstGit” as well. Then hit ok to accept that remote repository, and ok again to close the repository settings window.
And now we are ready to upload our stuff to BitBucket.
On the repository’s main window, there’s a checkbox “Unstaged files”, click on it and it will move all of our files to the “Staged” window:

Now, click the “Commit” button on the top left and you’ll focus on a text area for you to type a commit message. You should type something meaningful about what you are uploading there, as it is what you’ll see on the commit on BitBucket and on each affected file in the file list.
For our case, you can type “Initial commit” which will suffice.
In Git, you first Commit the files you want, and whenever you are ready, you Push them to your repository. Files are not uploaded until you Push them, so don’t forget to actually Push.
To avoid that mistake, check the “Push changes immediately to MyFirstGit/master”, so you avoid the step of pushing. Then hit “Commit”.
That’s it. Once the Commit and Push process is done, all your files are uploaded into BitBucket.
Now, the process is the same for every change you make:

You work on some feature

Then you go to SourceTree and open your repository

Stage your files

Enter the commit message

Commit and Push your files

Git has much, much more power than simply backing up files.
It stores every single change on your code, so you can easily see what you changed, and go back to any previous version of a single file, or an entire version altogether.
You can also work with branches, which makes it extremely easy to be working on version 1.1 of your game, and when there’s a hotfix you need to make, you can save your current 1.1 version and work on a 1.0b, wrap it up, and then get back with your 1.1.
Not to mention when there is more than one person working in the team. That’s the whole reason Git exists, for team work.
You can check awesome tutorials here: https://www.atlassian.com/git/tutorials/
And that’s not all! MonoDevelop greatly benefits with Git. When you open a MonoDevelop project that has a Git repository, you get a bunch of helpful options on the bottom of the MonoDevelop window:

Options of MonoDevelop with Git

Simply by clicking the “Changes” button you can see what changed between your local version (on the right), and the Base version (on the left). And you can select which version you want to use as your base and go back in history as much as you can. “Blame” shows each change and who made it.
Setting up Git is the first thing we do when starting a project and has saved us many times.
I hope it does the same to you.
Don’t forget to follow us on twitter for news regarding articles and game development [twitter-follow screen_name=’indelvestudios’]