In this guide I’ll walk through the basics of reverting commits. A handy command to review previous commits is “git log” — this will show you the history for all existing commits along with the notes associated with them.

There will almost certainly come a time in your development career where you will need to revert changes to fix a bug or remove a feature, and you will want to load back to a previous version. Version control with Git makes this easy, but there are a few things to keep in mind. You might not want to reset a project where you commit a change and that is now the head of the project, and the rest of your team members lose a lot of work. There is a workflow for creating revision history that will help manage this. One of the great things you can do with Git is to treat each commit as its own branch, and be able to manage changes to that commit independently from the rest of the project. This process makes it easy to both verify the point in time in which you need to revert a change, and to remove just that single change. This is an important reason why you want to be committing often in your projects.

Using the command “git switch [hash code]” you can revert your project (and Unity interface) temporarily back to the point of that commit in order to verify if that is indeed the point you wish to go back to. You will see in Unity that the instance has been reverted to that point in time. Now you are free to undo this commit without impacting the rest of the project. Isn’t that great? No full reset of all your hard work just because you made a bug 3 months ago. To go back to the present state, simply enter “git switch master” and no harm done, you are back to the present state of the project.

Another way you can use the commit hash is to create a new branch from that commit. You might want to do this if you need to restart from a certain point in the project. A safe way to do this is to use “git checkout -b [name-for-new-branch] [hash code]

This allows you to continue building from that point in the project on a new branch, while leaving the rest of your project intact and separate. This is superior to resetting the current project because it allows you to keep any new and unrelated progress from other elements of your project.

6. Reset your project to a previous state. Sometimes you might want to fully reset a project to a previous point. Bring up “git log” again. Browse this list for the specific commit you wish to revert to and copy the hash code. Once again, this is a hard reset and will delete any commits chronologically following the commit you have selected as the reset point. Enter “git reset — hard [hash code]” and this will move the “head” for your project back to the commit associated with the hash code you entered.

Now, you have told your local system to update the head to that point, but to update this on the server you need to force it. Enter “git push — force origin master”. You will see your changes push, and if you refresh GitHub you will notice all of your other commits have disappeared. This is why this is a risky process, because you will lose the rest of the data and you may regret the decision to reset back to this point. It is better to use the previous system for reverting commits one at a time and branching off if necessary.

Hope you enjoyed this tutorial and found it informative. Now that we have the Git basics down, let’s get into game development!

--

--