When using Git for version control, git log is a powerful command for viewing the commit history of the repository. If you want to view the first commit, there are several methods you can use.
-
Using the Default Behavior of
git log: By default,git logdisplays all commit records in chronological order, with the most recent commit shown first. To view the first commit, you can simply scroll through the output until you find the last entry, which is the first commit. This method is practical when the commit history is short. The command is:bashgit log -
Displaying Commits in Reverse Order: If the commit history is long, manually scrolling to the last commit may be inconvenient. In this case, you can use the
--reverseparameter to display commits in reverse order, so the first entry shown is the first commit. The command is:bashgit log --reverseThis command lists all commits, but the first commit is displayed first. You can view this entry or use other commands like
headto display only the first entry:bashgit log --reverse | head -n 1 -
Using
git rev-list: Another direct way to find the first commit is by using thegit rev-listcommand, which lists all commit SHA-1 values. By using the--max-parents=0parameter, you can directly identify commits with no parent commits, which is typically the first commit. The command is:bashgit rev-list --max-parents=0 HEADThis command outputs the SHA-1 value of the first commit. With this value, you can use
git logorgit showto view the details of this commit:bashgit log -1 <SHA-1> git show <SHA-1>
Each method has its applicable scenarios, and you can choose which one to use based on your specific needs. If you only occasionally need to find the first commit, using git log --reverse may be the most intuitive approach. If you frequently need to find it or automate the process in a script, using git rev-list may be more efficient.