Git: How to find and recover previous versions

Ian Plumpton
4 min readApr 24, 2021

--

In this third article on Git, I will go through how to find and restore previous commits if necessary.

So you’re working on the save/load system for your project and everything is going well until all of a sudden, nothing works, your code is generating dozens of compile errors and you’re not sure where you went wrong.

Luckily, you’re a savvy developer and have been making plenty of commits to your branch as you’ve worked on the project. Git provides the power to locate previous commits, restore them to get rid of any subsequent changes, or just to create a branch based on a previous version.

First up, how to locate the commits. In Git, make sure you are on the master or main branch, then use the command ‘git log’ to show previous commits on that branch.

Each commit has a unique 40 digit hash code that allows you to manipulate it in whatever you need. For instance, if you just want to switch to that a particular commit so you can view the files in Unity when the project had no errors, for example, you can switch to it in a similar way to how you would switch branch; with the ‘git checkout<hash code>’ command.

As you can see, Git tells you that you are in ‘detached HEAD’ mode where you can look at files and make changes and even commit them, but they won’t affect any current branch. You can also see that we can use the command ‘git switch -c <new-branch-name>’ to save the commit to a new branch which will preserve any changes you make.

To return to the branch you were working on, use the command ‘git switch<branch name>’.

You can automatically create a new branch from a previous commit with the command ‘git switch -c <branch name> <commit hash code>.

From here you can treat it like any branch; work on it, commit, push.

Now let’s say you want to return your master branch to a previous commit permanently. It’s almost never the solution to do this, but you may need to one day.

Firstly, you need to change to the master branch with ‘git switch master’. You will then need the hash code for the commit you want to revert back to. You can use ‘git log’ again to show them, or you can go to your repo in Github and click ‘X commits’ in the top right of the file structure.

This will show the commits for the repo. You can grab the hash code from the right side, once you have identified the correct commit.

Back in Git you can then use the command ‘git reset — hard <hash code>’ (space and double hyphen before ‘hard’) to restore the master branch to that commit. Finally you need to use the command ‘git push — force origin master’ (space and double hyphen before ‘force’) to force push the previous commit to the branch.

In Unity terms, this will revert the project back to the point when that commit was pushed to the server. Any changes after that point will be lost. This is very much the ‘nuclear’ option and should only be used in extreme cases of broken code. Normally, creating a branch and exploring bug fixes that way is the better option.

So that’s about it for Git. There is much more to learn, and I highly recommend you check out the Git documentation here for more information or if you get stuck, but this should be enough to get you version controlling your Unity projects properly.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Ian Plumpton
Ian Plumpton

Written by Ian Plumpton

Software developer in the field of Unity and C#. Passionate about creating and always driven to develop.

No responses yet

Write a response