Working with Git in Unity
To start off you need to create an account in GitHub where all of your projects will be stored and can be managed.
Now that the account has been created, we can download got from https://git-scm.com/downloads.
Here you are going to select download within the screen of the monitor while the website will automatically choose the OS version for your use. Download and install the application into your desired location.
During the setup, you need to select the second option and type ‘main’.
This step is not compulsory and you can go with the first option but do note that in 2020, GitHub replaced the default initial name for their new repositories from ‘master’ to ‘main’ so for those who are new to git and choose not to override the default name, if not careful, might end up getting errors due to mismatch between name used on your computer and online.
If have already installed the git and facing the problem mentioned then you can do the following to solve them:
Method 1:
In GitHub, you can go to your repository settings and change the default name of the branch back to “master”.
Method 2:
Change the default local branch name to “main”
There are other methods as well but these two methods should be easy enough for you to start
After the git is successfully installed, you can launch it by going to start menu and searching for Git and open Git bash.
To navigate through your Git Bash:
Type ‘ls’ and hit enter
This is going to show you all the files and directories inside your current directory.
If you want to go inside another directory then you can type ‘cd directory_name’ to enter that directory. You can also type initials and press Tab so the command line automatically completes the directory name for you.
you can also type cd and hit enter to return to the root directory.
To start working with git repository, you need to first start off by making a repository in the GitHub.
To create your repository, you can go to your profile in the GitHub website and go to Repositories tab. There you can see ‘New’ button on the right. Press that button and it will take you to the next page.
Give a name to your repository and write a description so that the function of the repository is clearly understood.
You can set your repository to public if you want anyone to access them and to private if you want only the selected people to access the repository.
As we will be working with unity, it is a good practice to add unity in .gitignore so that unity does not add unnecessary files in the repository.
After the repository has been created, you need to click on code and copy the git link so that we can link the repository to the project in the command line.
To link your repository with your project, you first need to initialize the git in your project. To do that you can navigate to the project folder or you can simply go to your project folder from outside the command line interface and right click and select Git Bash Here.
Here, you can type ‘git init’ command and press enter. This command is simply going to initialize git repository in your project and reinitialize if already initialized.
Now you can enter ‘git remote add origin [right click and paste the copied link here]’. This line of command is going to automatically add repository-test repository to our local server.
Here, Origin is a shorthand name for the remote repository that a project was originally cloned from. It is used instead of that original repository’s URL making referencing much more easier.
Now that we have connected our repository to our project, we need to know if the connection has been set or not. So we enter ‘git remote -v’ which is going to check the connection between the project and the repository by showing that you have permission for push and fetch action.
We now know that the connection is set and we are now ready to use git for any commit actions. The most important thing to remember at this point is that you have a server and it is going to contain things. So instead of committing and ignoring everything else happening in the server, it is a good practice to follow steps to avoid conflicts during merging.
In order to avoid the conflicts later down the line, it is best to first pull from the remote then commit and finally push to the remote server.
One of the most important aspect of git is branching. How does it work? Let us consider you are working on a project and have been committing it but now you got a new idea and want to try it out without affecting the project. This can be done by creating a branch which can exist on its own and you can change whatever you like without affecting the main project. If you want to add the changed you made to the main project then it can be done by merging.
If you want to see all the branches available in your repository then you can type ‘git branch’ and it will show all the branches in the repository. Among the branches, the one with a ‘*’ symbol on the front is the one you are currently at. You need to do the pull action first if you want to see the branches since the information has not been pulled from the repository.
The ‘git pull origin main’ command fetches a copy of the master branch from the original repository and merges it with the current branch you have checked out. To put it simply, It pulls the master branch from the remote called origin into your current branch.
Now if you type ‘git branch’ again, you can see that a branch named ‘main’ has been added to the git with a ‘*’ symbol upfront indicating you are currently in main branch
We have already done the pull action and for commit we need to know the current status of the repository. we type ‘git status’ which is used to display the state of the repository and staging area. It also allows us to see the tracked, untracked files and changes.
Here, we can add an individual files using ‘git add [filename]’ command. It selects that file and moves it to the staging area, marking it for inclusion in the next commit.
Alternatively, we can use ‘git add .’ to add all modified and new (untracked) files in the current directory and all sub-directories to the staging area, thus preparing them to be included in the next git commit.
After this step, you can type ‘git status’ to see all the files ready for commit in all green and if any files remain then it will be in red like before.
Now you can officially commit your files using ‘git commit -m “message”’. It will save all the staged changes, along with a brief description from the user,in a “commit” to the local repository.
The -m stands for message. It allows you to add short description of the changes being committed.
After you commit, if you look at the repository; nothing changes. This is because to make a change to the repository, you need to perform the final step i.e. push
Enter your username and press OK. Similar tab will appear and enter your password there and press OK again to finish your push.
After the successful push, you can reload the GitHub repository and there you can see all the changes made in the local server present.
As you can see, we can use ‘git branch [branch_name]’ command to create new branches where we can make change to different things without affecting the main project. When you are working in a team, it is best to create branches so that there will not be conflicts during the merging process.
You created a new branch but you still need to access that branch which can be done by ‘git checkout [branch_name]’ command. In the second picture, you can see that your current branch changed from main to script
Alternately, you can also use ‘git switch [branch_name]’ command to do the same thing.
Now let us say that we have created a inventory script in the inventory branch, committed it and pushed it to the remote. If you want that script to be in the main as well then you can use the ‘git merge [branch_name]’ command to integrate changes from another branch. The target of integration is always the currently checked out branch
We till now know how to create branches and merge them but what if you want to revert the progress to a point of particular commit. You can use ‘git log’ to get info on all the commits done in that branch
Note:
if there is a situation where you are stuck inside a log, you can press q and it will quit you to the command line
Now if you want to revert back to the point in a commit let’s say during initial commit then what I can do is create a new branch using ‘git checkout -b [branch_name] [commit_checksum]’ and it will create a branch named [branch_name] and time frame during [commit_checksum]instead of resetting to that time frame and losing all progress.
If you really want to reset your project to the commit timeline then you can use ‘git reset -- hard [commit_checksum]’. It takes the current branch and reset it to the point during [commit_checksum]. All the work done after that commit will be lost.
After git reset, you need ‘git push --force origin main’ to reset the remote server repository to the reset point.
It is the best option to use branch revert instead of hard reset to prevent any progress and data loss.