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
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:
- Create a new branch to start working with the desired version and avoid erasing the next versions of the branch (Recommended).
- 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.
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:
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).
Once that I verified it was the correct version, I returned 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:
We can also verify the new branch in the list of branches from our repo using the git branch command:
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:
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:
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:
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):
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.
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.