If we mess up something and know that it was working before two or three commits. We often want to go to that previous commit and check it out.
we will see how to do that using git commands,
There are three possible scenarios,
- Just go to the previous commit and then come back to latest commit (temporary jump)
- Go back to previous commit and modify some code there but don't want to lose the current update history too
- Go back to previous commit and discard all the new updates after that.
Just go to previous commit and then come back to latest
This is probably the easiest one. The steps to follow are,
git stashto stash any uncommited changes
git logto check the commit hash for the previous commit you are looking for
git checkout <commit-hash>
This will automatically go to the commit and show the stale branch. Once you finish what you want to look for in the code, you can once again go back to latest by running
git checkout masteror any other branch name if you are in different branch
- If you did stash any uncommited changes, then run
git stash popto pop the uncommited changes from the stash array
Go back to previous commit and modify, but keep the latest commits somewhere
This is typical situation where you don't want to lose your new commits but want to check and fix something in the old commit.
You can do that same thing until get the commit hash for the previous commit and then,
Checkout a new branch from the previous commit,
git checkout -b branch-name <commit-hash>
By this way, you can keep both the latest commit and separate branch to debug and fix from the old commit.
Go back to previous commit and discard all the latest commit after that
Again it is simple to do,
git reset --hard <commit-hash>
This simply reset to the old commit point and discard all new commits. One disadvantage with this approach is, you lose the history of all latest commit. Potentially you might lose some relevant code.
This command also won't work as expected if you have uncommited changes, so stash it before running this.
There is one more way to achieve this using
git revert. If you know exact number of commits you want to go back in history, then you can do so by,
git revert HEAD~3 # here 3 is the number of commit you want to go before to the latest commit
One advantage with revert is, it keeps the history and create a new commit to revert all the changes. You can even create your own message by not commiting automatically when doing revert.
You can also revert selective commits using commit hashes.
git logand find all the commits you want to revert. Copy those commit hashes
git revert hash1 hash2 #... and so on
By this way, you can selectively revert some of the commits and keep the code with proper commit log and history.