Open In App

How To List Only The Names Of Files That Changed Between Two Commits?

Last Updated : 18 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

rerew
How to list only the names of files that changed between two commits

Comparing 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

Next Article
Article Tags :

Similar Reads