Viewing the History and Changes
As defined earlier, Git is a time machine for your code. Now that you know how to make commits, you need to know how to read that time travel history and see what you modified.
View the Commit list: git log
To see the complete chronological record of all commits made in the project, use:
git log
This command will show you an information block for each commit, similar to this:
commit 8a4c1b9f7d2e3f5a0b1c2d3e4f5a6b7c (HEAD -> main)
Author: Juan Perez <juan@ejemplo.com>
Date: Mon Oct 30 14:25:01 2023 -0500
Add the contact page and form
- Hash (8a4c1...): A unique alphanumeric ID generated for that commit. It is the "license plate" of that exact version in time.
- HEAD -> main: Indicates where you are positioned right now.
- Author and Date: Who did it and exactly when.
- Message: The description you put in
git commit -m.
Tip to exit Log: If the history is very long, Git will put a colon (
:) at the bottom of the screen. PressEnterto scroll down one line, the space bar to scroll down a whole page, and theqkey to Quit and return to the normal terminal.
Improving the Log view
Sometimes git log gives too much visual information and causes you to lose perspective if you have hundreds of commits. You can ask for a summarized list of one line per commit:
git log --oneline
Output:
8a4c1b9 Add the contact page and form
3f7c9a2 Refactor the Header component
1a2b3c4 Initial commit (base architecture)
View changes in the code: git diff
git status tells you what files changed, but does not show you the code modified inside. To see line by line what you added or deleted before making a commit, use:
git diff
The result groups changes file by file:
- Deleted lines appear in red and start with a
-sign - Added lines appear in green and start with a
+sign
It is the best way to ensure that you didn't leave a forgotten console.log() or that you actually modified what you wanted to modify before doing git add ..