‘Git and Unity’ Guide — Part 2

5. Linking a unity project through Git Bash

Let’s go to your Unity project folder. Then, Right-click → ‘Git Bash here’

Okay! Now we are in Git Bash!

Before I dive deeper, first, I want to share with you a few useful Git commands that are widely used.

*Useful Command-List

  • git add — add changes from the working directory to the stage
  • git commit — saves changes to the local repository
  • git status — show status of the working directory and stage
  • git log — show the commit logs
  • git push — upload local repository content to a remote repository
  • git pull — upload a local repository from a remote repository
  • git branch — list, create or delete branches
  • git checkoutorgit switch—switch to another branch and check it out into your working directory

You can also see the command list from Git Bash by typing git — -help.

  • For more details, there is a cheat sheet created by GitHub.

https://education.github.com/git-cheat-sheet-education.pdf

6. Create a new repository

To start, we will first need to create a repository.

  1. Write git init on the command line.
  • git init— initialize the git to communicate & track this project

As shown above, ‘initialized ’ means git has created a new repository with a default branch named ‘ master’.

7. Linking Github to our local unity project

Let’s link Github to our project folder. Go to your unity project on Github.com. Then, click on ‘Code’ button and copy the URL address of your git repo. Now, go back to Git Bash and write git remote add origin [Your URL address of your git repo]on the command line.

  • git remote add origin [Your URL address of your git repo]— link Github to a local unity project

To verify if repo was added, write git remote -von the command line.

8. Before committing

Before we perform our first commit, I want you to keep in mind that there can be a merge conflict if we do not check what the changes are made on the server and follow the correct order of command.

From the server, always first ‘pull’ → ‘Commit’ → ‘Push’

  • fetch — Only download contents and references from a repository (not update a local repository)
  • pull — Fetch and download the latest content from a remote repository and update a local repository
  • commit — save a content change to the local repository
  • push — Upload a local repository content to a remote repository

9. Committing for the first time

Ok, as we discussed above, let’s first pull the changes from the server by typing git pull origin main.

‘HEAD’ = latest commit by default

Next, type git statusto check the status of the project. you will see a bunch of red texts… these are showing missing files. In order to add every file, type git add .

Type git commit -m “Created new unity project”.

  • ‘-m’ means a message. When you are working in a team, this will help to track the changes that were made.

Now, let’s push this commit. If you only type git push, it will not work as we need to specify the origin. Type git push origin master to push to the origin master branch.

Perfect, we successfully pushed it!

10. Creating new branches

When working in a team for a project, there may be a need for having several branches such as the following examples:

  • ‘Master’ branch — The latest update that is being released to the public.
  • ‘Dev’ branch — Internal development team use; track bugs and update before releasing to ‘Master’ branch.
  • ‘Inventory’ branch — Used specifically when making an inventory system on RPG game, etc.
  1. To create a branch, type in git branch [name of branch]. In my case, I want to create a branch named ‘dev’. I will type in git branch dev.
  2. To check, write git branch.
  • You will see two names pop up; dev and master. ‘Master’ is currently highlighted in green to indicate that we are in the ‘Master’ branch.

3. To switch branch to ‘Dev’, write git checkout dev or git switch dev.

  • If you type in git branch, you will see that ‘Dev’ is highlighted to let us know we are in the ‘Dev’ branch.

11. How to work with branch + Unity project

I will show you quick and simple usage of branch with unity project.

To demonstrate, in Git Bash window, let’s first switch the branch over to ‘dev’.

Then, let’s head over to Unity. Create a C# Script by right-clicking within Assets window → create → C# Script. Name it as ‘DevBranch’.

Back in Git Bash window, type in git status. What do you see? you will notice the red color cs files named ‘DevBranch’ — Basically means that there was a change made in the local repo that has not been committed yet.

Let’s add this change and commit.

  • Type in git add .to add the change.
  • Then, type in git status to check the status — it’s showing as added with green texts.
  • Lastly, type in git commit -m “added DevBranch script"to commit the change with the message “added DevBranch script”.

Although we committed a change, this will only take an effect within ‘Dev’ branch; other branches will not be affected.

  • In order to check, let’s return to ‘Master’ branch in Git Bash. Then, return to Unity. Eh? The C# Script ‘DevBranch’ that we created a moment ago has disappeared! It’s because we are in ‘Master’ branch, NOT in ‘Dev’ branch.

If you return back to ‘Dev’ branch, the C# script file will show up again. Hope this helps understanding the concept of branch using unity and git Bash.

That is all for today! Thank you so much for reading!

--

--

--

XR Unity Developer / Designer / Architect

Love podcasts or audiobooks? Learn on the go with our new app.

What I learned Nov 16th — Nov 22nd

Robot Framework API Testing POST Request

A human-centered approach to building a next-gen operating system for work (condensed version)

OTP in Elixir: Learn GenServer by Building Your Own URL Shortener

Take back control of your cloud: 4 ways to automate your cloud file management

Change the default port in Spring Boot

Setting up signal-cli on Linux

If you are a business that has cloud based information management system chances are that you have…

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Du Young Yoon

Du Young Yoon

XR Unity Developer / Designer / Architect

More from Medium

Static and dynamic libraries in C

C++ 3D Viewer Code with Explanations

Strings and its memory allocation in .NET

Pangram in C#