Ryan KubikBlogTwitterGames

When was this file added to version control?

May 11, 2019

2 min read

Git provides a lot of information to you about the history of your project. Much of this information can be accessed using git log.

By default, running git log will show you all the most recent commits on your branch in reverse chronological order. Meaning, the most recent commits show up first.

There are a lot of useful options you can use to adjust how git log functions though.

#Filter Commits

--diff-filter option allows you to filter the results of your git log command's output. There are a lot of different options for how to filter. The 3 most useful (in my non-expert opinion) are:

  • A - Added
  • D - Deleted
  • M - Modified

These will filter out all commits that do not have a file added, deleted, or modified.

git log --diff-filter=A

#Get File Names Changed

--name-only will print all files involved in a given commit in addition to the original commit message.

git log --diff-filter=A --name-only

#Target a Specific Directory

Git commands generally let you pass a trailing -- <path> option to narrow down the files you wish to target. git log is no different.

git log --diff-filter=A --name-only -- target/directory

#Remove Commit Body

If you've got a lot of commits to search through, you might not need to see all of the commit messages in your git history. You can use the option --pretty=oneline flag to only show the commit hash and title of your commits instead. Pretty has many options and you can get even more complex, custom formatting with the --format flag.

git log --diff-filter=A --name-only --pretty=oneline -- target/directory

Our final command will search target/directory for when all files were added to the git repository. These will be sorted with the most recently added files first.