Git

Listing Files Affected by a Git Commit

Listing Files Affected by a Git Commit

The Question

How can you view all files that were modified, added, or deleted in a specific Git commit?

Solutions

There are several ways to list files affected by a commit, each providing different levels of detail:

  1. Basic File List To get a simple list of filenames:
   git show --name-only COMMIT_HASH

This shows commit metadata, the commit message, and a list of affected files.

  1. File List with Status To see how each file was affected (added, modified, or deleted):
   git show --name-status COMMIT_HASH
  1. Condensed Output To remove the full commit message and show only a summary:
   git show --name-only --oneline COMMIT_HASH
   git show --name-status --oneline COMMIT_HASH
  1. Files Only To display only the file names without any commit information:
   git show --name-only --pretty=format: COMMIT_HASH

Note the empty format string after --pretty=format:.

Tips and Variations:

  • Replace COMMIT_HASH with the specific commit hash you’re interested in.
  • Use HEAD instead of a commit hash to see changes in the most recent commit.
  • Add ^ after a commit hash (e.g., COMMIT_HASH^) to see files from the parent commit.

Use Cases:

  • Code reviews: Quickly identify which files need attention.
  • Documentation: Generate lists of changed files for release notes.
  • Automated testing: Focus tests on affected areas of the codebase.

By using these commands, you can efficiently review the scope and impact of specific commits in your Git repository.

Would you like more information on analyzing Git commit contents?

Suggested Articles

Leave a Reply

Your email address will not be published. Required fields are marked *