Lab 10: Getting Previous Versions in Git

In this tutorial I will show you how to checkout any previous snapshot in the working directory.

Here’s how it can be done.

Get a List of Previous Commits

Use the git log command to get a list of previous commits. Instead of using git log we can use git hist to set a pretty summary of previous commits. We set up aliases in the last lab.

Lab 9: How to Setup Aliases and Shortcuts for Git Commands

git hist

You will notice that the commit summary for every commit begins with a hash code. Copy this hash and use it in the next command. Just the first 7 characters would be enough.

Checkout a Previous Commit

Use the git checkout command to checkout any of the previous commits.

git checkout <hash>

The output of the checkout command explains the situation pretty well. Go to the project directory and check its contents. You will find that it has returned to the state after the commit which you just checked out. All changes after that commit has disappeared.

Now you can again return back to the HEAD i.e the last commit by checking out master.

git checkout master

master is the name of the default branch. By checking out a branch by name, you go to the latest version of that branch.

Here’s a screenshot of all commands being executed.

Getting previous version in git

This content has been helpful to you?

Thanks for contributing!

Yes No