Referencing commits
As you continue to make changes and commit them, Git keeps a detailed history of these commits. To see this history, we use the git log
command.
Run the git log
command to see the commit history.
Tip: Some Git commands, such as git log
, may put your terminal into pager mode, which is designed for browsing long lists. In this mode, you can scroll through the output using the and arrow keys on your keyboard. To exit pager mode, press q.
You should have seen blocks of something like this:
commit 346ca091076783c70623aba03fb7139d3d27134f
Author: Your Name <you@example.com>
Date: 2024-03-15
Added H1, HTML, and BODY tags to hello.html
Here, 346ca091076783c70623aba03fb7139d3d27134f
is the commit's SHA-1 hash, a unique identifier for that particular commit.
Many Git commands require a commit reference as an argument to pinpoint exactly which version of the code you're interested in. For instance, git diff
, git show
, or git revert
often need to be launched with a commit ID as an argument to function properly.
There are multiple ways to refer to commits. Here are the most common ones:
- The full SHA-1 hash (e.g.
346ca091076783c70623aba03fb7139d3d27134f
). - A shortened SHA-1 hash (the first few characters, usually the first 7, for example
346ca09
). - Tags. These are human-readable names (e.g.,
v1.0
) that can be assigned by you to specific commits. We'll cover tags in just a bit. - Branch names. These are pointers to the latest commit in a branch. Branches (e.g.,
main
,new-dashboard-feature
) are the parallel lines of development in Git. We'll cover branches later as well. - HEAD. This is a special reference that points to the latest commit in the current branch.
- Relative references (e.g.,
HEAD~2
, which means the second commit beforeHEAD
, orHEAD^
, which means the commit before the latest).