The Magic of Version Control — Part II Frequent Git Commands

Andrea Zilio
3 min readApr 2, 2021

--

In the last post about version control we installed Git and created a remote repository on GitHub. We also used git init to create a local repository.

Today we will be looking at other frequent git commands on a new unity project. But before let’s understand how version control works.

How version control with Git and GitHub works

Version Control with Git

What makes it a bit confusing is that we cannot directly commit the files we have been working on to the local repository. Instead we need to add them first to the “stage” with the add command and only then, can we commit them to our local repository. From there we can then push the current state (commit) to our remote repository. With pull we both fetch the current branch from the remote repository and we merge it with our current branch.

When working with Git these are the commands you need frequently:

  • git init — creates our local repository
  • git remote add origin [remote URL such as GitHub URL] — adds the remote repository to you local repository
  • git add [file] and git add . — adds a file or all files (with the dot) to the stage
  • git status — shows modified files, staged for your commit
  • git commit -m “text to explain what you did” — saves what is on stage to your local repository and adds the text in the quotation marks
  • git log — shows the commit logs
  • git push — uploads the local repository to the remote repository (GitHub)
  • git pull — downloads the remote repository to the local repository
  • git branch — creates, lists or deletes a branch
  • git checkout — allows you to switch branches

Setting up a local repository and connecting with the remote GitHub repository

For our next steps we need to create a new Unity project (if you are not using Unity any other project folder will work) as well as a repository on GitHub . The following steps will then create a local repository, link it to the remote repository and push the current “main” (master) branch to the remote repository.

1 $ git init
2 $ git add .
3 $ git commit -m “first commit”
4 $ git remote add origin [remote URL]
5 git push -u origin main

Everything is ready now and we have both the local and remote repository in sync. As we continue to work we will need to repeat step 2 whenever we add files and then commit our changes locally as well as push them to the remote repository.

If others are working on the same project or if you working from different computers use the pull command to fetch and merge the state of the remote repository to your local repository.

Common problems with GIT

Large files — tell Git to ignore some files

As some Unity files are rather big and will not change, I would recommend that you create a gitignore file before you add your files at step two. You can find one here and modify it based on your needs:
https://github.com/github/gitignore/blob/master/Unity.gitignore

Main vs master branch

GitHub’s new default branch is “main”, while Git’s default branch is still “master”. To solve errors resulting from this you can either rename your main to master on GitHub or you can change the Git default to main using this code: git config — global init.defaultBranch main

As more and more companies discard terms such as “master” the second solution might be the better solution in the long run.

Authentication failure with push and two-factor authentication

If you enabled two factor authentication (2fa) you might get an error. The solution is a personal access token (PAT) created on GitHub. You can find a detailed description on how to do this here: https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token

--

--

Andrea Zilio
Andrea Zilio

Written by Andrea Zilio

Passionate Game Developer and Learning Expert. I love to create games and interactive experiences using Unity, Articulate, C#, JavaScript, PHP, HTML, CSS.

No responses yet