Search code examples
gitgit-diff

How do I find diff between two files on my local machine using git?


I have two files new.txt and new2.txt. I want to find the differences between these two files and was thinking to use git diff for the same but I couldn't. This is what I have done till now and respective errors -

  1. git init in the local directory.
  2. git diff new.txt new2.txt => produced no response
  3. Added both files to the git and then repeated same above command. => no response.
  4. git diff --cached new.txt new2.txt => found diff between new.txt and new.txt only before and after git add.
  5. git diff full_path_to_file/new.txt full_path_to_file/new2.txt => no response.

Solution

  • git diff is meant to do much more than compare two specific files, but it does have a mode in which it will do so:

    git diff --no-index new.txt new2.txt
    

    will do the job. You do not need to be in a Git repository. All of these arguments (--no-index and the two specific file names) are (sometimes) required (sometimes you can omit the --no-index).