How to use git's file diff outside of git repositories
- Published at
- Updated at
- Reading time
- 1min
I recently saw an interesting tip from Paul Irish on Twitter. He shared that he uses git diff
not only for reviewing git changes but also for general file diffing. If you use the --no-index
argument, you can compare any files. These files can be outside of git repositories, too.
This discovery is beautiful because I recently spent quite a bit of time tweaking my git diff
command using delta. And I love it when things look consistent in my terminal.
I added a new diff
command to my zsh setup. It overwrites the default diff
command and uses git diff --no-index
instead. In only does that, though, if there are exactly two command arguments. Otherwise falls back to the default diff
behavior.
function diff() {
if [ "$#" -ne 2 ]; then
command diff "$@"
return
fi
git diff --no-index $1 $2;
}
See below how my new diff fileA fileB
command looks like now!
Join 5.5k readers and learn something new every week with Web Weekly.