Building and Understanding Github & GitBash for Unity projects

Kenny Pruitt
Unity Coder Corner
Published in
7 min readFeb 14, 2023

Learning to use Git Hub & Git Bash was a little challenging, but by using it many times now I can really see the value in learning and understanding it well. The first thing that I had to do was to create a new Unity Project and create a new repository on Git Hub. One of the most important things I need to remember is if something isn’t working right, to read through the GitBash window, as it will usually indicate the issue going on.

Linking Unity and Git Hub

Then I opened Git Bash by right clicking in the project folder, to open Git Bash using that as the current directory. In Git Bash I started the process of linking the Unity project to Git Hub.

Initialize Git into the Unity Project by typing “git init” into Git Bash, and if I typed it in after it’s already there it will just initialize git into the project.

Link to the server by first creating the server by typing “git remote add origin” then added the URL from Git Hub. To verify the server was connected I could then type “git remote -v” and that should show that it can Fetch and Pull from the server.

During the process of linking the Unity project to Git Hub there was an issue I had with the default directory. While in Git Bash, if the branch is set to “Master” then it needs to be changed to “main”. To Do this all that is needed to do is type in “git config — global init.defaultBranch main:. Then I had to make sure to switch the current branch to the main branch as well by typing “git branch -m master main”. The blue text shown in the directory should say (main).

First Commit To Server

To make the first commit to the server there is a specific order it needs to be done in to prevent any issues, I must always do these commands: Pull, Status, Add, Commit, Push.

To Pull i type in “git pull” then press enter, then type “git pull origin main”. By doing this I pulled the project from the server first, this will ensure no errors come up if done exactly this way, because it will merge into my project.

Then I need to look at the Status of my project, by typing “git status”, this will show me what files I haven’t added by showing them in red. I can then add the files individually by typing “git add” and then adding the individual file name or I can add all the files by simply typing “git add .”.

After adding all the files, the next step is to Commit the project before Pushing it into the server. Every commit needs a message attached to it, this way I have reference to what each commit is later if needed. To do this I typed “git commit -m “add a message here””. These need to be done often to minimize bigger issues later.

The final step is to Push the project into the server, basically uploading it to the server. I typed in “git push origin main”. This basically is telling it to upload the main server. To verify I went on Git Hub and refreshed the page and saw that all my files had been successfully uploaded to the server.

If I wanted to check all my commits that I had done, I can always check on Git Hub by clicking the commits button.

Adding New Files

To add new files after I have added more to the Unity project, I simply follow the same steps as above: Pull, Status, Add, Commit, Push.

Creating & Switching Branches

Branching is the most important tool to be using with git. It allows me to make multiple branches of the project, as to not interfere with other parts of the project. The current active branch is always written in blue at the end of the directory.

Creating a New Branch: First I checked the current branches by typing “git branch”. Then to add a new branch I can simply add “git branch” with the name of the new branch after that. Then if I check again I will have a new branch with the name I assigned it.

Switching Branches: To switch between branches is very easy, I just type “git checkout” and add the name of the branch I want to switch to.

Using Branches

By using the branches effectively, I can have multiple different areas of a project being worked on at the same time without interfering with one another, all by switching which branch I am currently active on in Git Bash. All I need to do is switch the branch and go back into the Unity project and let it reload and I will be on that branch in Unity.

Merging Branches

If one of the branches that are created should always merge with another branch and share files with one another, all that is needed to be done is to switch to the branch I want to merge another with. Then type “git merge” and the name of the other branch I want to merge with.

Rolling Back Progress

If there is an error on a branch that has occurred, one way out of it is to roll back the current progress of the Unity project with a previously committed state.

First in order to do this, I need to pull up a list of all the logs from each commit that was submitted, by typing “git log”.

Next I can go select a commit to go back and check, by treating it like a branch and typing “git checkout” then adding the commit code for the specific log.

If I would like to go back and start working from a previous state, I could turn one of the logs into its own branch as well, simply by typing “git checkout -b” then adding a name for this branch and adding the commit code for the specific log.

Resetting The Main Branch To A Previous State

The easiest way to reset the main branch back to a previous state is to first go to Git Hub and go to the commits for the main branch. Then go back to Git Bash and make sure I am on the main branch, then type “git reset –hard” then the name of the commit code from Git Hub of a previous state.

Then this process needs to be forced to the server by typing “git push –force origin main”. This is dangerous to do though, because as you can see once you force this to the server, it removes all commits made after it.

Overall there is a lot to remember, but by continuing to practice and keeping to the process of committing to git often, will ensure many versions to fall back on in case any errors or conflicts come up with my Unity projects.

--

--

Kenny Pruitt
Unity Coder Corner

Unity Game Developer, C# Developer, and Software Engineer