Using GIT for version control with Unity pt.2
Objective: Create 2 new branches that will be used in development. Also, take these 2 branches and merge their updated files with each other as well as with the Main branch. Last, push the 2 developer branches with the Main branch updates into the GitHub remote repository.
We have the Main branch established which represents our final production state that the public will use. However, we need to work in a developer branch to improve our program or fix potential bugs that may arise later on. These branches could deal with an Inventory List or Quest representing some examples of branch names.
In this example the first branch we will create is the Dev branch which will be used by the developers to build the scenes. To create this branch, use the “git branch dev” commands. Use “git branch” to see the list of branches active in this program. The second branch we will create is name Scripts which will be used to create scripts on the player, non-player characters, or game objects. Now switch over to the Dev branch using “git checkout dev” commands.
With us in the Dev branch inside GIT BASH’s local repository, go to Unity’s Version Control Demo project. We created a simple area for the player to move in, as this will represent the level in the scene.
Back in GIT BASH, use the “git status” command to check for any new changes made. As we see, the Assets/Scenes/SampleScene.unity has new changes. Let’s accept these changes by using the “git add .” to add the files to be committed. Then, commit these changes with a message using the (git commit -m “Created a new level.”) commands. Next, we will push the Dev branch to our remote repository on GitHub by using the “git push origin dev” commands.
Go to GitHub and see that the Dev branch has been added to our remote repository. The Dev branch will also have all the new changes made in Unity and committed in GIT BASH uploaded here.
Now there are 2 ways to switch between branches. One way is to use the “git switch scripts” commands where if you go into “git — help”, there we will see the SWITCH command given as Git’s choice of command. Another way to switch is to use the “git checkout scripts” as CHECKOUT is the industry’s standard command of choice to use to switch branches.
To use the Scripts branch, we want to add the level created from the DEV branch into the SCRIPTS branch. To do this, make sure we’re in the SCRIPTS branch and use the “git merge dev” commands to MERGE the DEV changes into the SCRIPTS branch. Go back to Unity where we should see a dialogue box asking us if we want to Reload the page. We will click on Reload to reload the Unity editor with the SCRIPTS new changes added from the DEV branch.
Under the SCRIPTS branch inside Unity’s editor, let’s create a Player object from a capsule. Also, create a new C# script name Player and attach it to the Player object. This script was created to allow the player to move around the area. I’m not going into the script’s code as it’s not important for this demonstration.
Head back into GIT BASH under the SCRIPTS branch. Check the status for any new updates made, add those new files updates, commit them with a message informing of a new Player and C# script created. Then, push these changes to the remote repository under the SCRIPTS branch name.
Back in GitHub, we will see the SCRIPTS branch added to our remote repository with its updated changes.
Just in case the developers working on the DEV branch need to use the Player to move around the updated levels created, we’re going to MERGE the DEV and SCRIPTS branches together. Switch to the DEV branch, and use the “git merge scripts” commands to MERGE the SCRIPTS updates into the DEV branch. Now that the DEV branch has the most recent updates, we can MERGE this branch with the MAIN branch to make this the final results at the moment.
Now PUSH the MAIN branch into the remote repository, refresh the page, and see the new updates included inside this branch.
Go back in Unity, reload the editor, and play the game with the updates included from the MAIN branch.
Last thing we need to do is switch back to the DEV branch in GIT BASH, and PUSH this branch to the remote repository on GitHub. Check the DEV branch on GitHub by refreshing the page, and see the new updates from the SCRIPTS merge added onto the DEV branch.