Version control is a service that syncs up your projects online so that you can restore and branch your projects and collaborate with other developers.
Here’s a high-level description of how version control works.
- Creating Unity Project First
- You first make a Unity project before preparing to move it to version control.
- Creating Repositories
- You begin using version control by signing up on Bitbucket.org and creating a repository there.
- You then clone the repository to a folder on your computer using the Sourcetree program.
- You commit your first files to it, starting with a .gitignore file that tells Git not to upload temporary files that waste space in Unity projects such as the Library and Temp folders.
- You will share the repository by inviting your teammates.
- They will clone it and begin making commits.
- Resolving Conflicts
- When multiple team members try to commit changes, they may run into merge conflicts where more than one person has changed the same files.
- You can resolve this by comparing the files and merging the changes or deleting one of the versions and keeping “theirs” or “yours” instead.
- If you work on parts of the game that you want to keep separate till they are ready to be merged into the main branch, create one branch for each team member to work on or one branch for each major feature being developed.
- Inevitably, something will go wrong and you’ll want to revert to a previous version of the project. You can do that from within Sourcetree by discarding your changes and restoring to a previous commit state.
Gitlab has unlimited free private repositories with up to 10GB files each, which is the best value of all version control hosting services available.
- Create Gitlab accounts for all team members.
- Download SourceTree.
- You have to each use Sourcetree to generate private key files.
- Store private key files on OneDrive or Dropbox or somewhere secure.
- Upload those keys to your Gitlab user profile.
- Gitlab will recognize your pushes as being authorized.
While Sourcetree doesn’t yet have automatic wizard-like support of gitlab, you can already set it up to work perfectly with gitlab, or any other git server that runs over ssh or https. Here’s how you do it:
Step 1: Browse to your repo and copy the SSH or HTTPS address. I recommend SSH for Sourcetree.
Step 2: Click the Clone / New button.
Step 3: Type or paste the repo address into the Source Path / URL box. Choose where you want to store it locally in the Destination Path box. Click the Clone button.
Step 4: Enjoy your freshly cloned repo with an origin remote already set up for you.
CREATING YOUR FIRST SOURCE CONTROL REPOSITORY
Checked with version: 5
Screenshots made with: SourceTree for Mac (Windows version looks slightly different)
By the end of this lesson, you will have a local Git repo connected to a remote Git server, with a Unity project in it.
In this lesson, you will create a repository using the following
- Bitbucket: A free repository for small projects
- Git: A popular source control system, integrated with Bitbucket.
- SourceTree: A user-friendly GUI that works with Git and Bitbucket.
These tools are good for SCM beginners because they enable fast setup and easy use.
To get started, you’ll need an existing Unity project on your local machine. If you do not have one, complete the following tutorial: Space Shooter Tutorial
1. DOWNLOAD SOURCETREE
Go to www.sourcetreeapp.com to download and install SourceTree.
2. CREATE A BITBUCKET ACCOUNT
Visit bitbucket.org and sign up for a free Personal Account.
When asked to create your first repository, ‘No thanks’. We will be creating a repository with SourceTree.
3. USING SOURCETREE, CREATE A REPOSITORY ON BITBUCKET.
Open SourceTree and enter your Bitbucket login username and password when prompted.
After logging in you will be prompted to “Clone” your repo (to “Clone a repo” is to copy all the relevant files from a repository to your local machine). If you have not created a repo associated with your Bitbucket account, the list should be empty. Click “Skip Setup”.
After clicking “Skip Setup”, you will see the Repository Browser window.
We will create a remote repository using the Bitbucket account you previously created. Click “+ New Repository” in the Repository Browser. You will see a dropdown list. Choose ‘Create a remote repository’.
You will be prompted with a “Create a remote repository” window.
Use the following configuration settings:
- Account: Bitbucket Account
- Owner: Bitbucker User name
- Name: Any
- Description: Any
- Type: Git
- This is a private repository: Checked
You will be brought back to the “Repository Window”. Click on “Remote” and you should see the name of the repo you just made in the list.
4. CLONE YOUR REPO
From the Repository Window | Remote window, click on the Bitbucket repository you created in the previous steps. The following prompt will appear.
- Source URL: Leave as-is, but take note of it, as you will use it later when configuring Unity Cloud Build
- Destination Path: Choose a location on your local machine where repository files will be stored. Note that the Destination Path must point to a directory that has no items in it.
- Name: Any
- Advance Options: Leave as-is
After, you will be brought back to the Repository Window | Local, where you will see the local copy of the repository you just created. Note that the repository is empty; you will populate it in the next steps.
5. POPULATE YOUR LOCAL REPO WITH YOUR UNITY PROJECT
From the Repository Window | Local, double click on your repo. Enter your name and email when prompted.
You will now populate your local repository. Find your Local Unity Project and move all files to the repo Destination Path you defined in Step 4. In the following screen shot, the unity project “MyFirstProject” will be moved into the local repository folder “MyFirstUnityRepo”
Upon populating your local repo, SourceTree will automatically detect changes and will update, as follows:
6. COMMIT AND PUSH YOUR LOCAL REPO TO THE REMOTE REPO
Unity Cloud Build monitors your remote repo. To do so, you must COMMIT and PUSH your files to the remote repo.
Select the “Unstaged files” check box to select all files that will be pushed to the server.
NOTE: You’ll notice three dots (•••) to the right of each filename – click these dots to access a ‘more’ menu. Use this menu to select ‘Ignore file’ for your Unity ‘Library’ folder (Unity Cloud Build will construct the Library folder during the build process). You can also ‘Ignore’ any other files that aren’t required by Unity to build your project. Not completing this step will make the build process slower that necessary.
Click “Commit” on the top of the screen. Enter your name and email if prompted.
Enter in comments and click “Commit”
The following should appear: “There is nothing left to commit”
Click “Push” to copy all committed files to the Remote Repository.
Click “OK”. This will initiate the upload process.
7. CHECK YOUR REMOTE REPO
Go to bitbucket.org and login with your credentials. You should see your repository name listed on the first screen. Click it’s name to go to the page about your repo.
Once you’re on the repo page, go to the address bar in your browser and copy the url / address you see there.
Sharing your repository with your teammates
Once you put files in the repo, Bitbucket will show a button below text that says “Invite users to this repo”.
- Click Send Invitation.
- Enter your teammates’ emails or Bitbucket usernames.
- Click to send the invite.
- Your teammates will receive emails notifying them that they should open the repo on Bitbucket.org.
- Your teammates should click the link in the email to go to Bitbucket.org.
- Your teammates should make sure they have also installed Sourcetree.
- Your teammates can click Clone to open the repository in Sourcetree and choose a directory to save the files to.
- Your teammates can open the project folder in Unity.
Version Control tips for Unity
Commit each version that works
Try to commite regularly and small changes at a time. Focus on getting a single thing working, then commit it. There is no harm in having many commits. If everything breaks it will make life much easier.
Save and close Unity before commiting or pulling
Make it a habit to save the scene and the project before each commit. Many times there won’t be any changes to them, but sometimes there are. Things like code are automatically updated just by being built. But any changes to prefabs are only saved when you save the project. If you change a prefab, double check that it shows up in the Files in the working tree panel (they have a .prefab extension). If not, you forgot to save the project first.
Using repos with Cloud Build
You can use this repository to set your project up with Unity Cloud Build, a service that automates making mobile builds when you commit new versions to the repository.
This will be the URL that you give Unity Cloud Build when you add a new project. Click the ‘Git’ tab on this page, or follow the instructions in the Unity Cloud Build setup wizard to add an SSH key to your Bitbucket account to allow access.