Detect Changed Files with git

There are numerous reasons to want to know which files have been added or modified in a git repository, one of which is your text editor highlighting those files. Another use case is running tasks against only files which are presently changed, like lint or other validation routines.

So how can we identify files which are added or changed? Like this:

git ls-files --others --exclude-standard ; git diff-index --name-only --diff-filter=d HEAD ;

And if you only want to run a routine on a certain portion of files, you can use a regular expression to do so:

{ git ls-files --others --exclude-standard ; git diff-index --name-only --diff-filter=d HEAD ; } | grep --regexp='[.]js$'

The MetaMask team uses the following to run linting on only changed files:

{ git ls-files --others --exclude-standard ; git diff-index --name-only --diff-filter=d HEAD ; } | grep --regexp='[.]js$' | tr '\n' '\0' | xargs -0 eslint --fix

Tricks like this are so useful and reliable; you put them in place once and don’t consciously think about them again — and that’s OK. Set it and forget it!

  • How I Stopped WordPress Comment Spam

    I love almost every part of being a tech blogger:  learning, preaching, bantering, researching.  The one part about blogging that I absolutely loathe:  dealing with SPAM comments.  For the past two years, my blog has registered 8,000+ SPAM comments per day.  PER DAY.  Bloating my database…

  • Chris Coyier’s Favorite CodePen Demos

    David asked me if I’d be up for a guest post picking out some of my favorite Pens from CodePen. A daunting task! There are so many! I managed to pick a few though that have blown me away over the past few months. If you…

  • Page Peel Effect Using MooTools
  • Save Text Size Preference Using MooTools 1.2

Leave a comment

Your email address will not be published.