lab 12 Getting Old Versions

Goals

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 01

Execute:

git hist

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

Output:

$ git hist
* dc44f2e 2013-10-06 | Added a comment (HEAD, master) [Ismail Dhorat]
* 5ec54fd 2013-10-06 | Added a default value [Ismail Dhorat]
* e606a20 2013-10-06 | Using ARGV [Ismail Dhorat]
* 5d95e74 2013-10-06 | First Commit [Ismail Dhorat]

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.rb file.

Execute:

git checkout <hash>
cat hello.rb

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 from mine, whenever you see something like <hash> or <treehash> in the command, substitute in the proper hash value for your repository.

You should see …

Output:

$ git checkout 5d95e74
Note: checking out '5d95e74'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b new_branch_name

HEAD is now at 5d95e74... First Commit
$ cat hello.rb
puts "Hello, World"

The output of the checkout command explains the situation pretty well. Older versions of git will complain about not being on a local branch. In any case, don’t worry about that for now.

Notice the contents of the hello.rb file are the original contents.

Return the latest version in the master branch 02

Execute:

git checkout master
cat hello.rb

You should see …

Output:

$ git checkout master
Previous HEAD position was 5d95e74... First Commit
Switched to branch 'master'
$ cat hello.rb
# Default is "World"
name = ARGV.first || "World"

puts "Hello, #{name}!"

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

Table of Contents