top of page

Streamlining Unity Development with Git: A Guide to Version Control

Updated: Aug 1, 2023

A Guide to Version Control with Unity

GitHub

Version control is an indispensable tool for modern software development, and Unity projects are no exception. Git, a popular version control system, allows developers to collaborate efficiently, track changes, and manage project versions effectively. In this blog post, we will explore the use of Git commands in Unity to streamline the development process and ensure a smooth workflow for your team.


Before diving into Git commands, let's ensure that you have Git installed on your system. If you haven't already, download and install Git from the official website (https://git-scm.com/). You can also use Sourcetree, a GUI Based Git client. But here we will use Git SCM with Unity.


After installing Git SCM you can open Git Bash terminal by the following process: 1. Windows OS a. Open the Start menu and search for "Git Bash."

b. Click on "Git Bash" from the search results.

OR

a. Open the folder or directory where you want to open the Git Bash terminal.

b. Right-click in the folder (ensure you're not clicking on any file or folder icon).

c. From the context menu, select "Git Bash Here."


2. Mac OS

a. Open the Finder.

b. Navigate to the desired folder or directory.

c. Right-click (or Control-click) on the folder.

d. From the context menu, select "Services," then choose "New Terminal at Folder."


Now, the Git Bash terminal will open, and you can start using Git commands in the selected directory. The terminal allows you to perform various Git operations, manage version control, and collaborate with team members efficiently.


To start using Git in your Unity project, open your project folder in your preferred terminal or command prompt. Initialize a new Git repository with the following command:


git init

This command creates a hidden .git directory in your project folder, where Git stores all version control data.


Now to avoid cluttering your repository with unnecessary or generated files that are not relevant for version history or collaboration, we need to create a .gitignore file. It's a configuration file used by Git to specify which files and directories should be excluded from version control. When you add a file or directory to the .gitignore list, Git will ignore changes to these files, and they won't be tracked in your version control system.


To create a .gitignore file for your Unity Project, you need the following commands:


echo "Library/" >> .gitignore
echo "Temp/" >> .gitignore
echo "obj/" >> .gitignore
echo "*.csproj" >> .gitignore
echo "*.unityproj" >> .gitignore
echo "*.sln" >> .gitignore

For Project Introduction or Documentation, you can also create a README.md file by the following commands:


echo "# Your Unity Project README" >> README.md
echo "This repository contains my awesome Unity project." >> README.md

Now that your repository is set up, let's begin tracking changes to your project files. To add files to the staging area, where Git keeps track of changes to be committed, use the following command:


​git add <filename>

For example, to add all files in your project, use:


​git add .

Once you've added the changes, commit them with a descriptive message to indicate the purpose of the commit:


​git commit -m "Initial Commit!"

Now create your remote repository, open your Git hosting platform (e.g., GitHub, GitLab, Bitbucket) and sign in to your account. Look for an option to create a new repository, usually labeled as "New," "Create," or "+." Follow the on-screen instructions to set up the repository. You might be asked to provide a name, description, and other optional settings for your repository. You can also add .gitignore and README.md file from there.


By default, when you create a remote repository, the default branch of your Git repository usually use the old branch name to "master". You can see this by the following command:


git branch

But it's a common practice in the software development community to rename the default branch from "master" to "main" to reflect more inclusive terminology. So we will do this by the following command:


​git branch -M main

Now to link your local repository to your remote repository, use the following command to set the remote URL:


git remote add origin <remote_repository_url>

Replace <remote_repository_url> with the URL of your newly created repository. This URL can usually be found on the repository's page on the Git hosting platform. For example, for GitHub, the URL format is typically https://github.com/<username>/<repository_name>.git.


You can also create remote repository first, then create a local copy of a remote repository by this command:


git clone <remote_repository_url>

Now to push your changes to the remote repository, use:


git push -u origin main

Well Done! You've successfully integrated your Unity Project with your Git Repository. You can go to your Git Repository and see the changes.


Now, I won't be able to cover all the Git commands in this article, but I'll provide you with a list of the most commonly used commands in a project. I hope this guide proves helpful to beginners. If you need further clarification, feel free to leave a comment. Thank you for reading, and happy coding!

 

Getting & Creating Projects

Command

Description


git init

Initialize a local Git repository


git clone <remote_repository_url>

Create a local copy of a remote repository

Basic Snapshotting

Command

Description


git status

Check status


git add <file-name.txt>

Add a file to the staging area


git add -A

Add all new and changed files to the staging area


git commit -m "commit message"

Commit changes


git rm -r <file-name.txt>

Remove a file (or folder)

Branching & Merging

Command

Description


git branch

List branches (the asterisk denotes the current branch)


git branch -a

List all branches (local and remote)


git branch <branch name>

Create a new branch


git branch -d <branch name>

Delete a branch


git push origin --delete <branch name>

Delete a remote branch


git checkout -b <branch name>

Create a new branch and switch to it


git checkout -b <branch name> origin/<branch name>

Clone a remote branch and switch to it


git branch -m <old branch name> <new branch name>

Rename a local branch


git checkout <branch name>

Switch to a branch


git checkout -

Switch to the branch last checked out


git checkout -- <file-name.txt>

Discard changes to a file


git merge <branch name>

Merge a branch into the active branch


git merge <source branch> <target branch>

Merge a branch into a target branch


git stash

Stash changes in a dirty working directory


git stash clear

Remove all stashed entries

Sharing & Updating Projects

Command

Description


git push origin <branch name>

Push a branch to your remote repository


git push -u origin <branch name>

Push changes to remote repository (and remember the branch)


git push

Push changes to remote repository (remembered branch)


git push origin --delete <branch name>

Delete a remote branch


git pull

Update local repository to the newest commit


git pull origin <branch name>

Pull changes from remote repository


git remote add origin <remote_repository_url>

Add a remote repository


git remote set-url origin <remote_repository_url>

Set a repository's origin branch to SSH

Inspection & Comparison

Command

Description


git log

View changes


git log --summary

View changes (detailed)


git log --oneline

View changes (briefly)


git diff <source branch> <target branch>

Preview changes before merging

**Thanks to Joshnh for his wonderful repo.

16 views0 comments

Comments


bottom of page