How to use git’s file diff outside of git repositories (#snippet)

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…


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis

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!

git diff command showing a diff of two files in two columns


Reply to Stefan


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis


Print Share Comment Cite Upload Translate
APA
Stefan Judis | Sciencx (2022-12-08T15:36:38+00:00) » How to use git’s file diff outside of git repositories (#snippet). Retrieved from https://www.scien.cx/2020/10/17/how-to-use-gits-file-diff-outside-of-git-repositories-snippet/.
MLA
" » How to use git’s file diff outside of git repositories (#snippet)." Stefan Judis | Sciencx - Saturday October 17, 2020, https://www.scien.cx/2020/10/17/how-to-use-gits-file-diff-outside-of-git-repositories-snippet/
HARVARD
Stefan Judis | Sciencx Saturday October 17, 2020 » How to use git’s file diff outside of git repositories (#snippet)., viewed 2022-12-08T15:36:38+00:00,<https://www.scien.cx/2020/10/17/how-to-use-gits-file-diff-outside-of-git-repositories-snippet/>
VANCOUVER
Stefan Judis | Sciencx - » How to use git’s file diff outside of git repositories (#snippet). [Internet]. [Accessed 2022-12-08T15:36:38+00:00]. Available from: https://www.scien.cx/2020/10/17/how-to-use-gits-file-diff-outside-of-git-repositories-snippet/
CHICAGO
" » How to use git’s file diff outside of git repositories (#snippet)." Stefan Judis | Sciencx - Accessed 2022-12-08T15:36:38+00:00. https://www.scien.cx/2020/10/17/how-to-use-gits-file-diff-outside-of-git-repositories-snippet/
IEEE
" » How to use git’s file diff outside of git repositories (#snippet)." Stefan Judis | Sciencx [Online]. Available: https://www.scien.cx/2020/10/17/how-to-use-gits-file-diff-outside-of-git-repositories-snippet/. [Accessed: 2022-12-08T15:36:38+00:00]
rf:citation
» How to use git’s file diff outside of git repositories (#snippet) | Stefan Judis | Sciencx | https://www.scien.cx/2020/10/17/how-to-use-gits-file-diff-outside-of-git-repositories-snippet/ | 2022-12-08T15:36:38+00:00
https://github.com/addpipe/simple-recorderjs-demo