GitHub Guide

Recovering an older version of a GitHub project using Git Bash

A quick guide to turn back into a previous version of a project in GitHub using Git Bash

Fernando Alcantara Santana

--

Hi! In my last post I talked about how to handle branches of a GitHub repo using Git Bash and now it’s time talk about how to recover an older version of a project hosted in GitHub using Git Bash.

Have you ever entered in panic after running your perfectly clean and efficient code and noticed that the console displays 65410239428 debugging errors? Yeah I wouldn’t have read the number either :).

Well, if it’s hosted and properly committed in GitHub you wouldn’t have to worry if an older version of it was working fine as we can always get back to work in that specific version.

So, to start, we need to get into our repo directory using Git Bash like we have been doing since my first post. Once in there, we can choose between 2 options to return to an older version of our project:

  1. Create a new branch to start working with the desired version and avoid erasing the next versions of the branch (Recommended).
  2. Return to the older version erasing the next versions of the branch.

Create a new branch

By choosing this option we can always go back to our older branch and compare the code to see what went wrong after the version we choose to recover.

Inside the directory, where the older version was committed, we need to use the git log command to display the commits that we pushed into GitHub already. Once the commits are displayed we need to copy the id of the desired version to work.

Note: I’m using a Unity project connected to a repo from older posts as the example.

Select the id of the commit you want to recover
Select the id of the commit you want to recover

If you want to check if the version you are trying to recover is the one you need then you can use the git checkout command with the copied id to verify the state of that version:

Using git checkout with the desired id is a good way to verify if it’s the correct version
Using git checkout with the desired id is a good way to verify if it’s the correct version

As you can see in the next image, I returned to one of my first commits in the unity project (when I had just a single script).

This was my first change in the project pushed to GitHub
This was my first change in the project pushed to GitHub

Once that I verified it was the correct version, I returned to the master branch:

Use git checkout to return to the master branch
Use git checkout to return to the master branch

And now, in order to create, switch and give a name to the new branch from the older version we need to use the git checkout -b command with the new name and id of the desired commit:

Using the -b command allows to give a name to the new branch
Using the -b command allows to give a name to the new branch

We can also verify the new branch in the list of branches from our repo using the git branch command:

List of all the branches in my project
List of all the branches in my project

Once we recovered the older version inside our new branch it’s important to commit and push it to GitHub so the changes are reflected:

And that’s how you can recover an older version of your project inside another branch to avoid erasing other commits after it.

Return completely to the older version

By choosing this other option we can’t go back to the commits after the one we choose to check what went wrong.

As you can see in my repo, I have a total of 5 commits in my master branch:

This is the master branch of my Unity project
This is the master branch of my Unity project

So, if we ever want to return completely to an older state (to the first one in this case) we need to execute the git log command inside the project’s directory and get the id of the commit:

Copy the id of the commit you want to recover
Copy the id of the commit you want to recover

And, once we choose the version to recover, we need to execute the git reset --hard command followed by the id of the commit:

We can see that my Unity project changed to the older state when I didn’t even have a folder for the scripts:

This was the project’s first state
This was the project’s first state

Now, in order to reflect the changes in GitHub, we need to execute the git push --force command followed by the name of our server (which is origin) and the name of our branch (which is master):

Warning: pushing this changes will erase the next commits after the one recovered
Warning: pushing this changes will erase the next commits after the one recovered

Finally, we can see that the repo in GitHub displays that I have a total of 2 commits and that the last one I pushed is the 1st commit.

This is the recovered state in my master branch
This is the recovered state in my master branch

And that’s it! the older version is recovered and you can work with it using the method you prefer. I’ll see you in the next post, where I’ll be showing how to start with Unity and game development.

If you want to know more about me, feel free to connect with me on LinkedIn or visit my website :D

--

--

Fernando Alcantara Santana

A passionate computer technology engineer and Unity developer that is always looking to grow in every aspect of life :).