Listing changed files in a commit

In this post we demonstrate how to show the commit logs and names of changes files, or only list names of changed files, or only names and status of changed files in a Git commit.

List names of changed files and commit message in a commit

Use below command to list the changed files in a commit:

# List changed files in the specified commit
$ git show --name-only <commit>

# Examples

# List changed files in HEAD
$ git show --name-only HEAD
commit ea34837d870e48106ae9ad09f41297a64ad6a6a1 (HEAD -> master)
Author: xxx <xxx@xxx.com>
Date:   Wed Mar 13 20:27:46 2019 +0800

    feat: add localization

my-plugin.php
uninstall.php
lib/my-util.php

List only names of changed files in a commit

If you only want the file names, use:

# List only names of changed files in HEAD
$ git diff --name-only HEAD~ HEAD
my-plugin.php
uninstall.php
lib/my-util.php

# or
# --format controls the format of the commit logs
# --format='' spefify an empty string to avoid printing the commit logs
$ git show HEAD --format='' --name-only
my-plugin.php
uninstall.php
lib/my-util.php

diff can be used to cross multiple commits.

$ git diff --name-only <commit1> <commit2>

List only names and status of changes files in a commit

To list only names and status ( new or modified) of changes files, add name-status option:

# List only names and status ( new or modified) of changes files
$ git show HEAD --format='' --name-status
M my-plugin.php
M uninstall.php
A lib/my-util.php

The status of a changed file can be:

  • A, Added
  • C, Copied
  • D, Deleted
  • M, Modified
  • R, Renamed
  • T, have their type (i.e. regular file, symlink, submodule, …) changed.
  • U, Unmerged
  • X, Unknown
  • B, have had their pairing Broken

List names of changed files with specific status in a commit

You can just list names of files with specific status with --diff-filter option to specify the status (or status combination mentioned previous):

# List only the names of the files added and deleted
$ git show HEAD --format="" --name-only  --diff-filter=AD
lib/my-util.php
# List only the names and status of the files added and deleted
$ git show HEAD --format="" --name-status  --diff-filter=AD
A lib/my-util.php

# Or use lower cases of status to exclude such these status
# List only the names of the files changed but exclude the ones 
# added or deleted
$ git show HEAD --format="" --name-only --diff-filter=ad
my-plugin.php
uninstall.php

Resources

Leave a Reply