lab 8 Getting Old Versions
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 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 * 1e510db 2013-11-11 | Using argv (HEAD, master) [Jim Weirich] * 1cc627c 2013-11-11 | 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. Then check the contents of the hello.c
file.
Execute:
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 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 9416416 Note: checking out '9416416'. 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 9416416... First Commit $ cat hello.c #include <stdio.h> int main() { printf("Hello World!\n"); return 0; }
A “detached HEAD” message in git just means that HEAD (the part of git that tracks what your current working directory should match) is pointing directly to a commit rather than a branch. Any changes that are committed in this state are only remembered as long as you don’t switch to a different branch. As soon as you checkout a new branch or tag, the detached commits will be “lost” (because HEAD has moved). If you want to save commits done in a detached state, you need to create a branch to remember the commits.
Older versions of git will complain about not being on a local branch rather than being in a detached HEAD state. In any case, don’t worry about that for now.
Notice the contents of the hello.c
file are the original contents.
Return to the latest version in the master branch 02
Execute:
git checkout master cat hello.c
You should see …
Output:
$ git checkout master Previous HEAD position was 9416416... First Commit Switched to branch 'master' $ cat hello.c #include <stdio.h> int main(int argc, char *argv[]) { if (argc > 1) { printf("Hello %s!\n",argv[1]); } else { printf("Hello World!\n"); } return 0; }
‘master’ is the name of the default branch. By checking out a branch by name, you go to the latest version of that branch.