Get the Affected Files from a GIT Commit

Ever wanted a list of all those files that got changed in your last commit?

Get a list of affected files from your last commit

So, you made your GIT commit but now you want to figure out, most likely later on, which files were affected by a certain commit. Well fret no more! It’s actually a pretty useful one-liner that you could even redirect out for a list of those files. So, let’s start with the command itself.

git diff --name-only HEAD <Hash ID of commit>

Let’s break that command down. You’re doing a diff but asking for only the files, by name, that were affected between your current “HEAD” state and a specific commit you made which is represented by above. You can get this hash id by running the command:

git log --oneline

This will print out a list of recent commits, with the hash id up front. I highly recommend making an alias to this command. I personally use: glo - which is set in my home .bashrc file. Once you have the list displayed in your terminal, you should be able to discern which commit you’re interested based on your comments. You can use the “q” key to quit out of the pager and then run the original command against the target hash id.

Where is it useful

If you need a list of files affected to upload somewhere i.e. your site using your favorite FTP editor, this is a pretty quick way to identify the files. You could also go a step further and supply them to an rsync call to do the upload for you! We’ll cover rsync in another article. Hopefully this has helped you!

Think I might be a good fit for your project?

Let's get the conversation started!