Skip to content

lab 13 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

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
* 1dee7f9 2022-10-24 | Tell user how many names they have (HEAD -> main) [Jim Weirich]
* c72af6b 2022-10-24 | Rename variable to match its usage [Jim Weirich]
* 8cdd2cd 2022-10-24 | Can specify multiple names [Jim Weirich]
* 28fe396 2022-10-24 | Added a comment [Jim Weirich]
* 15c7573 2022-10-24 | Added a default value [Jim Weirich]
* 7d55044 2022-10-24 | Using ARGV [Jim Weirich]
* 91b926e 2022-10-24 | First Commit [Jim Weirich]

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 to see what that commit changed.

Execute:

git show <hash>

Output:

$ git show 91b926e
commit 91b926ef01fd87e82d603834821cab7c88877ed9
Author: Jim Weirich <jim (at) edgecase.com>
Date:   Mon Oct 24 19:01:59 2022 +0000

    First Commit

diff --git a/hello.rb b/hello.rb
new file mode 100644
index 0000000..af0c87b
--- /dev/null
+++ b/hello.rb
@@ -0,0 +1 @@
+puts "Hello, World"

Now let’s checkout the commit and 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 91b926e
Note: switching to '91b926e'.

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 switching back to a branch.

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

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at 91b926e 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 main branch

Execute:

git checkout main
cat hello.rb

You should see …

Output:

$ git checkout main
Previous HEAD position was 91b926e First Commit
Switched to branch 'main'
$ cat hello.rb
names = ARGV || ["World"]

puts "Hello, #{names.join(" ")}!"
puts "You have #{names.length} names!"
puts "You have #{names.length} names!"

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