Introduction
In software development, tracking changes across different versions of a project is crucial for understanding modifications and maintaining an efficient workflow. Using Git as your version control system, you can easily identify which files have changed between two commits. This guide will walk you through various methods to list the names of files that were modified, added, or deleted between two specific commits in your repository.
Understanding Git Diff
The git diff
command is an essential tool for inspecting changes between commits, branches, and working directories. It can show differences at a granular level, including line-by-line changes within files. However, when you want to quickly see which files were affected without delving into the content changes, using specific flags with git diff
becomes invaluable.
Listing Only File Names
To list only the file names that changed between two commits, use:
git diff --name-only SHA1 SHA2
Here, SHA1
and SHA2
are identifiers for your starting and ending commits. The output will include relative paths of files along with their names. You can shorten the commit hashes as long as they uniquely identify each commit.
Example:
git diff --name-only HEAD~10 HEAD~5
This command lists changes between the 10th latest commit and the 5th latest commit.
Understanding File Status
If you need more context about what happened to these files (e.g., modified, added, or deleted), --name-status
provides additional insights:
git diff --name-status SHA1 SHA2
This will output a prefix indicating each file’s status:
- M: Modified
- A: Added
- D: Deleted
- C: Copy-edit (copied and modified)
- R: Rename-edit (renamed and modified)
- U: Unmerged (conflicts after a merge)
To see changes along with commit messages, you can use:
git log --name-status --oneline SHA1..SHA2
This command lists commits that occurred between two specified points (SHA1
to SHA2
) and shows the status of files changed in each commit.
Filtering Changes for Specific Files
If your interest lies in specific files or directories, you can refine your queries:
git log --name-status --oneline SHA1..SHA2 -- <filename>
This command filters changes to show only those related to the specified file(s).
For analyzing a single commit’s changes, use:
git log --name-status --oneline SHA1^..SHA1
Viewing Statistics
To get an overview of insertions and deletions across changed files, --stat
offers a summary:
git diff --stat SHA1 SHA2
For more detailed statistics including the number of lines inserted and deleted for each file, use:
git diff --numstat SHA1 SHA2
If you prefer just a brief count, --shortstat
can be employed:
git diff --shortstat SHA1 SHA2
Comparing Branches
To compare your branch with another, such as origin/master
, use the merge-base to find common ancestors and list changed files:
git diff --name-only $(git merge-base origin/master HEAD)
This command shows differences between your current branch and its common ancestor with origin/master
.
Bypassing the Pager
By default, Git commands output results in a pager like less or more. To avoid this when you need quick access to information:
git --no-pager diff --name-only SHA1 SHA2
This command outputs directly to your terminal without entering a pager.
Conclusion
Mastering these git
techniques allows developers to efficiently track changes and understand the evolution of their codebase. Whether it’s viewing file names, understanding file statuses, or obtaining statistical summaries, Git offers powerful options tailored for different needs. By leveraging these commands, you can enhance your workflow and focus more on development tasks.