Git - Getting Old Versions

From EdWiki

GitLab - Getting Old Version

  • Goals
    Learn how to checkout any previous snapshot into the working directory.

Going back in history is very easy. The checkout command will copy any snapshot from the repository to the working directory.

  • Get the hashes for previous versions
git hist

Note: You did remember to define hist in your .gitconfig file, right? If not, review on aliases.

Examine the log output and find the hash for the first commit. It should be the last line of the git hist output. Use that hash code (the first 7 characters are enough) in the command below. Then check the contents of the hello.c file.

git checkout <hash>
cat hello.c

Note: The commands given here are Unix commands and work on both Mac and Linux boxes. Unfortunately, Windows users will have to translate to their native commands.

Note: Many commands depend on the hash values in the repository. Since your hash values will vary, whenever you see something like <hash> or <treehash> in the command, substitute in the proper hash value for your repository.

The output of the checkout command explains the situation pretty well. Notice the contents of the hello.c file are the original contents.

  • Return the latest version in the master branch
git checkout master
cat hello.c

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