Lets learn how to checkout any previous snapshot into the working directory.
Going back in history is very simple. The checkout command can copy any snapshot from the repo to the working directory.
RUN:
git hist
Note: Do not forget to define hist
in your .gitconfig
file? If you do not remember how, review the lesson on aliases.
RESULT:
$ git hist
* fa3c141 2020-01-09 | Added HTML header (HEAD, master) [kwikl3arn]
* 8c32287 2020-01-09 | Added standard HTML page tags [kwikl3arn]
* 43628f7 2020-01-09 | Added h1 tag [kwikl3arn]
* 911e8c9 2020-01-09 | First Commit [kwikl3arn]
Check the log data and find the hash for the first commit. You will find it in the last line of the git hist
data. Use the code (its first 7 chars are enough) in the command below. After that check the contents of the hello.html file.
RUN:
git checkout <hash>
cat hello.html
Note: Many commands depend on the hash values in the repository. Since my hash values will be different from yours, substitute in the appropriate hash value for your repository everytime you see <hash>
or <treehash>
in the command.
You will see …
RESULT:
$ git checkout 911e8c9
Note: checking out '911e8c9'.
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 911e8c9... First Commit
$ cat hello.html
Hello, World!
The checkout
command output totally clarifies the situation. Older git versions will complain about not being on a local branch. But you don’t need to worry about that right now.
Note that the content of the hello.html
file is the default content.
RUN:
git checkout master
cat hello.html
You will see …
RESULT:
$ git checkout master
Previous HEAD position was 911e8c9... First Commit
Switched to branch 'master'
$ cat hello.html
<html>
<head>
</head>
<body>
<h1>Hello, World!</h1>
</body>
</html>
‘master’ is the name of the default branch. By checking out a branch by name, you go to its latest version.