How to revert or reset to a previous version using GitHub
In this article we will cover reverting or resetting by branch. Our example will continue to use the same Unity Project from the previous two articles on version control with Unity.
How to Setup Version Control for Unity using GitHub
Why use Version Control System?
When to revert or reset?
There can be various reasons you want to return to a previous version. You might encounter a bug in a local branch and want to undo that change. For this you use a revert.
Reset will set the project back to a previous state. Be very cautious when using reset, because it might create major problems for other members in your team and lose a lot of work. When you reset you basically choose a commit(version) and set that as the latest. With that action you also delete all commits after that in that branch.
It’s advised to always branch out and keep your work instead of resetting.
In this example we want to return to a previous version our “main” branch. To start off make sure we are our “main” branch. Use the git switch main command or check with git branch
Now we are in our “main” branch.The log function will show a list of all commits done on this branch.
These commits can be treated as branches.
Copy the commit name
Next switch to the commit. (Hit Q to exit the screen if needed)
git checkout COMMITNAME
(in our case commitname would be 4b82849aaa6d232fda99a411bdb29daa83fb2c1d) The checkout is essentially the same as the switch command. To ‘switch’ to commit you will have to use the checkout command instead.
Just like a branch you will be in that version. Go to Unity and verify if this is indeed the correct version you were looking for.
If you use git switch main you can return to the main branch and see nothing is lost in Unity.
Usually it most convenient to create a branch out of this earlier commit version and use that to continue on working from. We do that with:
git checkout -b newbranchname commitname
(ex. newbranchname = old-project-state
ex. commitname = 4b82849aaa6d232fda99a411bdb29daa83fb2c1d)
Another option is resetting from a previous commit. This will erase commits made after that point in the branch. Be very careful with this or you might lose a lot of work. Let’s restart from this commit:
git switch main (We want to reset to a previous commit in the “main” branch. So make sure you are in the right branch. )
git log (for more details see revert section)
Copy the desired commit name again.
Alternatively, you can get commit name from GitHub as well.
To reset we use:
git reset --hard commitname
Next we push this to the server.
git push --force origin main
Head over to GitHub and see how all main commits are lost.
Our other branches are still saved.
Again be very careful with resetting and just branch out and save your work.