How To List Only The Names Of Files That Changed Between Two Commits?
Last Updated :
18 Jun, 2024
Git is a powerful version control system that allows developers to track changes in their codebase. One common task is identifying which files have changed between two specific commits. This can be useful for code reviews, debugging, and understanding the impact of changes. In this article, we'll explore how to list only the names of files that changed between two commits using Git.
1. Using git diff
The git diff command is the primary tool for comparing changes between commits, branches, or the working directory. To list only the names of files that changed between two commits, you can use the --name-only option.
Basic Usage
To compare two commits and list the changed file names, use the following syntax:
git diff --name-only <commit1> <commit2>
- <commit1> and <commit2>: The commit hashes or references you want to compare.
Example
Suppose you want to see the files that changed between commit abc123 and commit def456:
git diff --name-only abc123 def456
This command will output a list of filenames that have changed between the two commits.
How to list only the names of files that changed between two commitsComparing Branches
You can also compare the changes between branches using the same command. For example, to compare the main branch with the feature-branch, use:
git diff --name-only main feature-branch
Comparing Working Directory with a Commit
To list files that have changed in your working directory compared to a specific commit, use:
git diff --name-only <commit>
This command shows the files that have been modified in the working directory but not yet committed, compared to the specified commit.
Comparing Staged Changes with a Commit
To list files that have been staged (i.e., added to the index) but not yet committed, compared to a specific commit, use:
git diff --cached --name-only <commit>
2. Using git diff-tree
Another useful command for listing changed files is git diff-tree. This command can show the differences in a tree format, but with the --name-only option, it can list file names.
Example
To list the files changed in a specific commit, use:
git diff-tree --no-commit-id --name-only -r <commit>
- <commit>: The commit hash you want to inspect.
For example, to list the files changed in commit abc123:
git diff-tree --no-commit-id --name-only -r abc123
3. Using git log
You can also use git log to list changed files. This can be particularly useful if you want to include additional information about the commits.
Example
To list files changed between two commits along with the commit information, use:
git log --name-only --pretty=oneline <commit1>..<commit2>
For example, to list files changed between commits abc123 and def456:
git log --name-only --pretty=oneline abc123..def456
Similar Reads
How to Show Files Changed Between Two Revisions in Git?
To show files changed between two revisions in Git, you can use the git diff command with specific commit references. Here's a step-by-step guide: Table of Content Using Git DiffUsing Git LogUsing Git DifftoolUsing Git ShowIdentify the RevisionsRun the Diff CommandView the DifferencesOptional: View
3 min read
How To List All Commits That Changed A Specific File In Git?
Git, a widely used version control system, allows developers to track changes in their codebase efficiently. One common need when working with Git is to list all commits that have modified a specific file. This can be particularly useful for tracking changes, understanding the history of a file, or
1 min read
Calculate Number of Lines Changed Between Two Commits in Git
Git allows developers to track changes and collaborate effectively. One common task is to determine how many lines of code have changed between two commits. This information can be useful for code reviews, project metrics, and understanding the scope of changes. In this article, we will walk through
3 min read
How to Ignore Files that have Already been Committed to the Repo?
In Git, keeping your repository clean and organized is crucial. Sometimes, you might have files that were accidentally committed but shouldn't be tracked by Git. This article explores effective methods to handle such situations and prevent these files from being tracked in the future. Table of Conte
2 min read
How to Commit Case-Sensitive Only Filename Changes in Git?
Git is a distributed version control system that is widely used for tracking changes in source code during software development. By default, Git is case-sensitive, but the underlying file systems (like NTFS on Windows and HFS+ on macOS) can be case-insensitive. This can cause issues when renaming fi
2 min read
How To Find The Most Recent Common Ancestor of Two Branches in Git?
In software development, managing code changes through version control systems is crucial. Git, one of the most popular version control systems, allows developers to create branches for features, bug fixes, or experiments. These branches can diverge from the main codebase and evolve independently. E
3 min read
How to See the Differences between two Branches?
When working on a Git repository with multiple branches, it's essential to compare the changes between different branches to understand the differences and merge them effectively. Git provides several commands to visualize the differences between branches. In this article, we'll explore how to see t
3 min read
How to Perform a Diff Between Two Stashes in Git?
Git stashing is a handy feature that allows developers to temporarily shelve changes without committing them. However, there might be scenarios where you need to compare the changes stored in different stashes. Git does not offer a built-in command to directly compare stashes, but there are several
2 min read
How to see the Changes in a Git commit?
Understanding the changes introduced in each commit is crucial for effective collaboration and version control in Git. Whether you are reviewing someone else's work or tracking your own modifications, Git provides powerful tools to inspect changes. This article will guide you through the various met
4 min read
How To Show The Changes Which Have Been Staged?
In Git, staging changes is a critical step that allows you to prepare specific modifications for the next commit. Viewing these staged changes helps ensure accuracy and precision in your commits. This article details various methods to display staged changes in Git. What Does "Staged" Mean in Git?In
2 min read