Git command to show files changed in a commit

How to see exactly which files have changed in a single commit

Recently I wanted to pull a list of changed files from an older commit from the command line. There are lots of reasons why you would want to do this after the fact, and you don’t always have access to tools like Github to see things visually.

Using git show to show files changed

Turns out, as with most things in Git, you can do this with a relatively short command.

git show --name-only {commit}

You can replace {commit} with the SHA you want to retrieve, or things like HEAD or HEAD^.

If you aren’t sure what those terms mean;

  • The SHA is the unique identifier used to identify a commit
  • HEAD is the last commit
  • HEAD^ the ^ suffix means “show me the parent of” whichever identifier you’ve passed in

To remove the commit message from the output, we can add the --oneline flag:

git show --name-only --oneline {commit} 
git show --name-status --oneline {commit} 

There are some slightly longer ways of achieving essentially the same thing, I will share them here for completeness.

Using git log to list files changed

git log --pretty=format: --name-only -n 1 {commit}

This command is much more involved.

We are telling git log to look more “pretty” by specifying a particular format. In our case we’re saying to only show the name (--name-only) and to only show the one specific commit (-n 1).

Where git log is better is when you want to show more than just that one commit.

Using git diff-tree to list files changed

git diff-tree -r {commit}

This will recursively (visit all folders) list the files that have changed, like git show we can pass some additional parameters to tidy the output up.

  • --no-commit-id will remove the commit ID output
  • --name-only will display only the names of files that changed, much like we had above

Show files changes in a commit on YouTube

I made parts of this blog post into a video to show this command in action.

Seeing what commits happen in a file

What if you want to do the opposite? If you have access to a file and you want to understand which commits made up the file as it currently is, we can use git blame.

In my opinion, git blame is not the best name for this, the point is to blame a commit, but when you run the command the author comes back too. Regardless, if you run the following code:

git blame path/to/file.md

It will return the contents of the file with a left margin showing;

  • the commit SHA
  • the author of the commit
  • the date it was committed

Very useful for getting more context.

Archiving changed files

We have an article all about archiving in git. Where we make a call to get the changed files and pass that into the git archive command to create a compressed file with our changes.

Recent posts View all

RubyFreelancing

Belfast Rails Supper Club

I recently attended the Belfast Rails Supper Club here are my thoughts and some more information about this meet up.

SEO

Google follows URLs in text

Today I learned that Google follows URLs even when they are plain text