Working with Github

Chris Didier
12 min readMay 3, 2021

Objective: Learn how to use version control.

Version control allows you to control your projects of code and working with other teammates on projects. This is more efficient than working with Unity Collaborate. The project leader normally decides what version control system the team is going to use.

To start off let’s go to https://git-scm.com/ and download the version that fits your computer and go through the download process and make sure git-bash is selected and click through the rest of the prompts default options.

Now that are download is complete we can go down to our search bar and search for Git and select Git Bash.

Once opened it will look like this.

Now we need to know how to navigate this terminal Over time you will learn the commands that you will be using like the back of your hand. Very often you will be pushing and pulling to and from git-hub.

One of the firs thtings we will have to do is navigate into our Unity projects folder. In my case my folder is on a different hard drive, but we will first run out ls command to see our directories.

To change hard drives type cd /D

Now type ls and Unity Projects folder is now available, but if we try to access the folder directly it fails because this command line does not read spaces.

To avoid this we put it in “ “.

We can also quickly populate where we want to navigate to by starting to type our destination and then press tab.

Now we are going to create a version control example to show how this works. So first we are going to create a new Unity project in our Unity-Hub, and navigate to that folder after completion in Git-Bash.

Once the project is created let’s navigate back to our projects folder.

cd /D/”Unity projects”

ls

cd “version control 2.0”

We are now in the folder of the project we are working with.

Now we are going to be creating our Git-Hub repository. Let’s go to github.com. We are going to create a new repository.

Click new in the top left corner of the screen.

First we give our repository a name, a description of what it is about, whether you want the project public (open to everyone) or private(you and those you invite), if you want to have a readme, a gitignore and have it set to Unity (ignores a lot of extra unity files to a project)

When complete click create repository and the screen below will appear.

We are now going to link our project to our Git-Hub repository. The first thing we want to do is get the path to our repository.

Next we want to access our folder in git-bash. You could just type in the path or you could go to file explorer on your computer and when you are at your desired folder you could right-click and select the option to open git-bash from there.

Next in Git-bash we want to type the command

git init

Press enter when complete

This initialize git to communicate and track our project. The next thing will be adding this webserver to our repository.

so type

git remote add origin + link address

To verify the process completed type

git remote -v

As you can see our connection has been completed we can now push and pull. This may also prompted you to login to git-hub. Do so if it does.

Now we are going to be completing our initial commit. It is very important to commit our project in a certain order to prevent any potential issues. You always pull from the server, you commit, and then you push. We are now going to do our first commit.

if we type

  • — help it will show the git commands we may potentially use.

So to get started we will enter the following command.

git pull

This downloaded some parts of the project, but it did not know what part to look into as we did not specify a branch. A branch is where we are currently working.

Git branch

Now Git-Hub has changed over time as command used to be git pull origin master, but the command you will now be using is

git pull origin main

As you can see there is going to be a Fetch_Head. Head is showing that is the most current version.

Now we will type git branch and it will show our master branch.

Now we need to see what we need to commit. So we are going to type

Git Status

As you can see they are red once they are committed they will turn green.

We want to commit our assets. So we will type

git add Assets/

As you can see the committed assets our green.

To add everything you will use the command

git add .

To push your commit and send it to git-hub type

git commit -m “Created a new unity project”

Now if you go to git-hub right now to see if your commit took place you will not see anything.

We need to type our destination of where we are pushing this commit to.

git push origin master

Now if we go back to github we will see our page is updated with our documents

As you can see if you look at the overview of the project you can see the initial commit and when we did our update.

So let’s head over to Unity and add to our project. We are just going to add a cube and add a challenge script. When complete head back over to git-bash and type

git status

As you can see it detects that there are new changes made.

Now before we upload anything new we must make sure that the rest of the team has not made any changes to the git. So we are going to pull from the branch we are working with.

As you can see we are already up to date and there is no new content.

We are now going to add the files we have so we will type

git add .

We will then do a git status to make sure the files are ready.

Now we are are going to add our commit and description of what we are committing.

git commit -m|”Created a cube with challenge script”

and now we are going to push it to the server

git push origin master

Now we need to make sure we have plenty of branches for our projects. This allows us to work in parallel of other members of the team. That way we can be working on development branches and finished branches.

If you type

git branch

You will see there is an * next to master letting you know this is the branch you are working in.

Now to add a new branch type

git branch dev

This creates a new branch and if you were to type git branch again you will see the new branch in it.

To switch to the dev branch we need to type the following command

git switch dev

and to see if we switched type

git branch

as you can see the dev branch is the active branch. Now anything I do while working in the dev branch will be independent of the master branch. When your project is finished being tested merge with your master.

We will now be creating an inventory branch and a quest branch as our game will have those functions within it.

So we will be typing

git branch inventory

git branch quests

and to make sure they were added

git branch

We are now going to be adding some items to one of our branches but not the others so lets go to our dev branch if you are not already

git switch dev

and then go into Unity and create a new devbranch scripts and save the scene.

If you go back to git-bash and type

git status

Now if I go back to branch master and check the status it will detect that there are certain things that need to be committed.

Now if I go back to my dev branch and create a commit from there.

git switch dev

We are going to add our files.

git add .

git status

Then we are going to create a commit.

git commit -m “Created dev script”

Now switch back to our master branch and you will see the script we added in our dev branch is gone from Unity.

This is because we are on our production ready branch is would not be there, but if we go back to our dev branch it would be there.

git switch dev

Now if we are going to work our development area and only push to the master when we need to we need to grab the dev environment first.

So if we are working on our inventory we will merge the dev section by

git merge dev

We will now be creating a new script called inventory on the inventory branch.

We are now going to check our status

git status

Then we are going to add it.

git add .

We are now going to process a commit.

Now if we were to go look at the server none of this would be there as there would be no commits.

If we were to go back to our dev branch the inventory script would not exist under that branch. So we want to merge our inventory back to our dev branch.

git merge inventroy

so if we do a git status we will see there is nothing to commit.

So we will now push our new commit.

git push origin dev

So if we go to github and look at our dev branch.

If you are on the team you can now work if the new dev branch and when you are ready to release this code into the working release we will switch to our master

git switch master

and then we will merge the dev branch with it

git merge dev

To update the master

git push origin master

To reset our branch to a previous time in history we will want to check the log.

git log

So say we want to go back in time to when we created a cube with a challenge script. first press the Q key to get out of that screen.

We want to select the commit code and copy it.

git checkout fcb0527309fc9f6d97ab663f5b535fe1251dc685

You can see it reverted back to when this was the setup.

Now if we switch back to our master you will see everything is back

git switch master

A good way if you want to use the position as a restart point is to turn it into its own branch and give it a name by

git checkout -b old-project-state fcb0527309fc9f6d97ab663f5b535fe1251dc685

Now you can build from the old project state and keep building other areas of the project at the same time. Now you can switch back and forth between the branches at any time.

Finally lets look on how to reset the master branch to a previous time in history. We want to go back to creating a new unity project. So we want the hash code provided when we use

git log

We are now going to reset our project back the when we created the project.

git reset — hard 6662f5f8846879511b3381eefd6d28027f9190b1

We now have to update our github so we must force a push

If we go back to github we can see all of our other pushes are now gone

It is often better to branch your changes instead of resetting our main branch.

--

--