Keywords: git | log | name-only | file changes
Abstract: This article explains how to use the `--name-only` flag with `git log` to show only the names of files that have been modified in commits. It covers basic usage, combining with other flags like `--oneline`, and alternative methods using `git show` for specific commits, suitable for developers to efficiently analyze code changes.
Introduction
Git is a powerful version control system, and the `git log` command is used to view commit history. In practical development, users may focus only on the names of files that have changed, without needing detailed commit information. By using the `--name-only` flag, `git log` can elegantly meet this requirement, enhancing workflow efficiency.
Basic Usage: The --name-only Flag
In Git, the command `git log --name-only` outputs only the names of files changed in each commit. This is a straightforward method where the flag defaults to listing file names after each commit while retaining basic commit details such as identifiers and messages.
Code example:
git log --name-only
For more concise output, combine it with the `--oneline` flag, which reduces each commit history to a one-line summary while still displaying file names. This is particularly useful in rapid workflows.
Code example:
git log --name-only --oneline
Displaying File Names for Specific Commits
For specific commits, use `git log --pretty="format:" --name-only <commit>`, where `<commit>` can be any commit identifier or tag. This command hides all standard output formatting and shows only changed file names, suitable for precise analysis.
Code example:
git log 73167b96 --pretty="format:" --name-only
Note that in code examples, if text contains elements like `<T>`, it should be escaped as `<T>` to avoid HTML parsing errors. For instance, `print("<T>")` is the correct representation.
Alternative Method: Using Git Show
Beyond `git log`, the `git show` command also supports the `--name-only` flag, especially useful for viewing file changes in a single commit. The usage is similar: `git show --pretty="format:" --name-only <commit>` to obtain a clean list of file names.
Code example:
git show --pretty="format:" --name-only 73167b96
This method is effective when inspecting specific code versions and integrates well with other Git functionalities.
Application Scenarios and Best Practices
Common scenarios for using the `--name-only` flag in development include quickly viewing file changes during code reviews, sharing commit updates in team collaborations, or analyzing project change trends. It is recommended to combine it with other flags, such as `--graph` to display branch structures, for more comprehensive information.
Conclusion
`git log --name-only` provides an efficient way to display only changed file names, filtering out unnecessary detailed information. By combining with options like `--oneline` and `--pretty`, output formats can be customized based on needs. Meanwhile, `git show` is suitable for precise learning of individual commits. Adopting these methods will help improve the efficiency and maintainability of Git workflows.