‘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 stagegit commit
— saves changes to the local repositorygit status
— show status of the working directory and stagegit log
— show the commit logsgit push
— upload local repository content to a remote repositorygit pull
— upload a local repository from a remote repositorygit branch
— list, create or delete branchesgit checkout
orgit 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.
6. Create a new repository
To start, we will first need to create a repository.
- 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 -v
on 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
.
Next, type git status
to 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.
- 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 ingit branch dev
. - 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!