When using Git for version control, **git logThe ** command is a useful tool that shows the relationship between commit history and branches.
Basic usage
Use the following command format in the terminal or command line to callgit log:
1
git log
This will display a list of all commit history, with the newest commit on top.
Limit output
git logSeveral options are provided to limit the output to meet different needs.
-oneline: Displays commit history in a compact one-line summary.
1
git log --oneline
-decorate: Display the reference names of branches and tags in the output.
1
git log --decorate
-graph: Use graphical representation to show branch and merge relationships.
1
git log --graph
These options can be combined:
1
git log --oneline --decorate --graph
when you go git log --oneline --decorate --graph command, the output shows the relationship between commit history and branches in a compact, graphical way. Here's an explanation of each part of the output:
Commit Hash: A unique identifier for each commit, usually using a short hash. These hashes are unique identifiers of commits and can be used to reference and retrieve specific commits.
Commit Message: A descriptive message entered during a commit to explain the changes and purpose of the commit.
Branches and Tags: Displays the reference name of the branch where the current commit is located and the associated tags. These reference names appear in parentheses after the commit hash, and as decorators before branches and tags **decorate**。
Graphical Representation: Use characters (such as slashes, backslashes, vertical bars, and asterisks) to represent branch and merge relationships. This section uses a graphical representation to show the development and merging of different branches in the commit history. Slashes (/) and backslashes (\) indicate branch development, vertical bars (|) indicate forking of branches, and asterisks (*) indicate merge points.
The output of the command can be read in the following ways:
Each line represents a commit and contains a short commit hash and commit message. For example: **579ac2d Resolved merge conflicts with master branch**。
The branch and tag reference names appear after each commit. They are enclosed in parentheses and preceded by the reference name tag: or HEAD -> identifier. For example: **(HEAD -> huiyu/product_search_similarity_test, origin/huiyu/product_search_similarity_test)** represents the current branch and remote branch.
Graphical representation shows branching and merging relationships. Merge commits appear as lines where one or more branches are merged together. For example, **\** and / Characters represent different branch merges. **|The ** character indicates a fork of a branch. This graphical representation can help you understand the relationships between different branches in your commit history.
By reading this output, you can learn information about each commit, including commit hashes, commit messages, branch and tag reference names, and branch and merge relationships. This helps you track the evolution of your code, how branches are merged, and the relationships between different branches.
Filter and sort submissions
There are options you can use to filter and sort your commit history.
-author=<author>: Show only commit history for a specific author.
1
git log --author=John
-since=<date>: Only show commit history after the specified date.
1
git log --since="2023-01-01"
-until=<date>: Only show commit history before the specified date.
1
git log --until="2023-02-01"
-grep=<pattern>: Only display commit messages containing the specified pattern.
1
git log --grep="bug fix"
-follow <file>: Track the change history of the specified file.
1
git log --follow file.txt
-reverse: Display the submission history in reverse order of submission time.
1
git log --reverse
branches and tags
By default,git logShow commit history for all branches. You can also specify specific branches or tags.
1
git log <branch-name>
1
git log <tag-name>
Other options
git logThe command also provides some other useful options, such as:
-stat: Displays brief statistics for each commit, including the number of files changed and lines inserted/deleted.
1
git log --stat
-pretty=<format>: Display commit history using a custom output format.
1
git log --pretty=format:"%h - %an, %ar : %s"
-graphand-onelineCan be combined with other options.