As we have successfully installed Git, this article will focus on basics of Git.
Once installed, you can start Git from Start Menu by clicking “Git Bash” or by right clicking in the project folder and click “Git Bash Here”.
The major difference is that you will need to guide Git through to your project folder if you start it from the Start Menu. I have decided not to get into the detail since I prefer starting Git Bash by right-clicking inside the project folder since it is quicker.
However, if you want to guide yourself through here is a short summary. To list the available directories, we simply type ls. To change the directory, we simply type cd (folder name) (without the brackets of course) until reaching our project folder.
Once you start Git from your Unity project folder, you will encounter a screen as following, make sure that you are in the correct path:
Git is actually very user friendly, even if you forget a command, you can list the available commands by typing git — help,which will list you all of the available commands. (Medium corrects two lines in front of help as one, it is actually two.)
At this point, I would like to remind how Git works. Simply, we need to build a bridge in between our local machine and hosting server. We have already created a new repository on the server (on the previous article), however, we do not have a local repository yet. Thus, we need to create a local repository first.
git init is the command to use to initialize a repository inside our Unity Project folder. Once you enter it, you will observe that a hidden .git folder created inside your project folder, which illustrates that we have successfully created a local repository.
Now, we need to link the local and online repository. Switch to your github.com account page and select your repository that you have created for the project. Click on Code — and copy the url. Typing git remote add origin https://github.com/yourname/yourrepository.git will link our local repository with the one on the server.
Ctrl+v will not work on git, so you will need to right click and paste the link by mouse. The “origin” used in this command just the name of the server that we liked to call. We could call it anything other than the origin, but origin is the one that is widely being used in the industry.
To make sure that we have successfully linked our repositories, we type git remote -v
The guideline to use Git is actually very simple. First, we need to “pull” data from the server to work on. Then, we need to notify the server that we have made some changes at our project by using “commit” and upload it to the server by using “push”. Pull-Commit-Push is always the rule to follow while using Git that will prevent us from making errors.
Since we have successfully linked the local and online repositories, we can now start with pulling. If you have used Git before, you need to know that there has been a change with the October 2020 update. With the update, Git replaced “master branches” with “main branches”. Thus, to pull, we simply type git pull origin main. Before the 2020 update, this command was being used as git pull origin master. I was unaware of this, and had some difficult time to sort it out.
Git can tell us if we have non-matching files by typing git status
As I have stated, Git is really user friendly. First of all, it states that we are currently on our master branch, which you can also see on the directory with blue. It states that we have not committed yet, but we have untracked files. Untracked files are represented with color “red” in Git. To add these files, we can either type git add (FileName) or just type “git add .” to add all of the files.
To check if we have successfully added the files, type “git status” again to see if the files are written in green. To commit, we use “git commit -m “Your-Commit-Note-Here””. This is really important, since on the later stages of our projects we may want to return to a commit point. Thus, we should make sure that our comment is clear enough.
Lastly, we need to push. To push, we type git push origin master. Here, we are pushing to master branch existing in the origin (the name we called to our server). I am aware of the confusion of pulling main and pushing master. Since we were on master branch, we pushed to master. Branching will be explained further in the next article.
Lastly, check your github repository’s master branch to see if it’s updated.
And we are all done, we have successfully pulled, committed and pushed our project into the remote server.