Tutorial 4
Inspecting & Rolling Back Commits
In this stage you will learn how to inspect previous commits and roll back changes. This tutorial continues from Tutorial 3.
Steps to Complete
- Open foo.txtand in the second line of text, “Glad you could join us.” change “us” to “them”.
λ notepadfoo.txt
- Stage and commit this change. Note that we can stage and commit with a single command by using the “-a” flag with the commit command.
λ git commit –a –m “Changed a word"
- Display all commits, most recent first by issuing this command:
λ gitlog
You should see three commits.
- (Read, no action required) To display exactly what was changed in a particular commit, use this syntax:
λ git show commitID
For the commitIDyou can copy the entire string from git log or you can usually just type the first four characters assuming they are unique among all the commit id’s. For more information, see documentation for git-show. The git diff command (and its variants: git diff-files, git diff-tree, git diff-index) are useful for comparing differences between commits.
- Show the changes associated with the third (most recent) commit (titled “Changed a word”). Be sure and use your commit ID:
λ git show commitID
The result is shown on the right.:
- Next, show the changes associated with the second commit (title is “Provided more info in foo”). Be sure to use your particular commit ID:
λ git show commitID
The result is shown on the right.
- (Read, no action required) To roll-back all changes to the state immediately after a particular commit, use this syntax:
λ gitreset --hardcommitID
In other words, the state will be exactly as if you had just made the commit with commitID. There are many other ways to use reset. For more information, see the documentation for git-reset.
- Roll-back the changes to the second commit (title is “Provided more info in foo”). You may need to git log to get the commitIDor scroll the window up.
λ git reset --hard commitID
The result will look similar to this:
- Verify that the working tree is clean, i.e. nothing to commit:
λ git status
- Verify that there are only two commits in the log:
λ git log
- gitkis a windows based Git repository browser. Display it now by typing:
λ gitk
The result will look as shown below.
For more information, see the documentation for gitk.
- Play with some of the options in gitk. For example:
- Select the “Old version” and “New version” radio buttons.
- Select the “new file added” node in the Commit Tree to examine that commit.
- Close gitk.
1