Using Git for Unity: Branch, Merge and Reset

Manuel Cabacungan
3 min readAug 4, 2021
git reset — hard <commit hash>

Objective: Manage the source control repository of a Unity project through the git commands for branch, merge and reset.

Branch

The git branch command allows you to create a copy of the current branch of your project to work on specific features, separate from the main branch which is usually what is released to the public.

git branch

  • Shows list of available branches and indicates the current branch with an asterisk (*)and green text.

git branch <new branch name>

  • Creates a copy of the current branch and gives it the the name in <new branch name>

git switch <other branch name> or git checkout <other branch name>

git log

  • Displays all the previous commits made in this git session using the default command-line text editor.
  • Use the up and down arrows to scroll through the log for the commit you are looking for.
  • To exit the editor, press Shift and Z together, twice.

Merge

The git merge command allows you to join the current branch with another branch. In the example we have been working with so far, we are in the Quest branch. In Unity, let’s add a Quests script.

In Git Bash, commit the change to the Quests branch.

After this, you can also push to the server.

But to Merge the Quests branch with the main branch, switch to the main branch and then use this command:

git merge Quests

Reset

Reverts the the current branch to a previous state. Reverting is actually very dangerous because you can lose files. Consider making a new branch instead. But this is how it works.

In our Quests branch example, let’s suppose, for some reason, we wanted to revert back to the beginning just after we created a new unity project. Use the git log command to get the hash of that commit.

Example hash code for the commit when we added the unity project: cd8be144829c796b49afc21be09d61cc090c2572

Remember, exit the log editor by pressing Shift and z twice.

Use the command git reset to force the files back to that commit:
git reset —hard <commit hash>

In our example, the command is this:
git reset —hard cd8be144829c796b49afc21be09d61cc090c2572

When you push to the Github server, the command is this:

git push — force origin main

There are definitely more git commands to help us manage our source control but these commands are a good start, and I’m looking forward to learning more.

--

--