Check if specific file in git repository has changed

26,720

Solution 1

You can pass a file or directory name to git diff to see only changes to that file or directory.

If you're "writing a batch file" then the --exit-code switch to git diff may be particularly useful. eg.

git diff --exit-code test

or:

git diff --exit-code path/to/source/dir

Which will return 1 if there are changes to the file named test (or any other specified file or directory) or 0 otherwise. (Allowing a script to branch on the result.)

To see the names of the changed files only (and not a complete diff) use --name-only xor you can use -s to get no output at all, but just the exit code.

Solution 2

Using

git diff test

will show the differences between the work directory test and the repository version. Using diff will show the actual differences; if you are not interested in those use

git diff --name-only test

Solution 3

Git doesn't provide methods to query history of a remote repository. Git commands are intended to be run against a local repository, so you would have to clone first (fetch would be cheaper if you've cloned once before). That said, there are some ways to get around this limitation:

  • You could ask your Git server to run the commands you want for you via some kind of API. For example, browsing GitHub webpages or using their developer API fall into this category. In this case, GitHub's web servers are running Git commands for you. If you're using a server other than bare Git, check to see if your server has an API that could help.
  • Use git-archive to download an archive containing parts of the repository. I don't think this will help you.
Share:
26,720
user2554585
Author by

user2554585

Updated on August 01, 2020

Comments

  • user2554585
    user2554585 almost 4 years

    I am novice in dealing with git, but I have a repository that contains a file named 'test'. I want to check if that specific file has changed. Is there anyway to do that?

    From a big picture, I'm writing a batch file that will perform a git clone of the repository if anything has changed (which I have figured out using the dry run option) EXCEPT for the test file(meaning that even if the test file has changed, I don't want to perform a git clone)

    Let me know if you need any clarifications and thanks for your time