48

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

user2554585
  • 3,489
  • 6
  • 20
  • 23

4 Answers4

75

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.

Rachel K. Westmacott
  • 2,038
  • 20
  • 15
  • 2
    Good answer, but for some reason `--exit-code` alone doesn't affect the exit code when diffing files marked as having conflicts, even if the files contain differences that show up during `git diff`. `git diff -s --exit-code $CONFLICTED_FILE` works, though. Seen in git 2.27.0. – amphetamachine Jul 14 '20 at 15:36
17

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
GoZoner
  • 67,920
  • 20
  • 95
  • 145
  • thanks, do you mind explaining the difference between your suggestion and: git fetch -v --dry-run – user2554585 Jul 23 '13 at 16:55
  • 2
    `fetch` is going to report on file-level differences between the remote and local repository. `diff` will produce line-level differences between the file in the local repository and in the working directory. – GoZoner Jul 23 '13 at 18:43
  • 1
    Adding `HEAD` after `diff` will also take into account changes staged for commit, which you may want. – Louis CAD Feb 05 '19 at 16:40
  • 2
    For sequential scripts you can use `git diff --no-pager` to prevent git showing the results in a pager – tbjgolden Feb 21 '19 at 04:02
  • I believe `--no-pager` has to be right after `git` like `git --no-pager diff` – animatedgif Oct 19 '20 at 15:46
3

As it was correctly mentioned in the answer of Peter Lundgren,

git commands are intended to be run against a local repository,

so git clone is likely to be called anyways.

On the other hand, if you need to check if you want to trigger some specific CI step, you might find useful something like this in your script:

if git diff --quiet $COMMIT_HASH^! -- . ':!test'; then
   echo "No significant changes"
else 
   echo "There are some significant changes, let's trigger something..."
fi

--quiet disables all output of the program and implies --exit-code (see git documentation).

Reference this answer for more details regarding the pattern at the end of the expression.

GoodDok
  • 1,770
  • 13
  • 28
1

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.
Peter Lundgren
  • 8,787
  • 2
  • 26
  • 21
  • Thanks for the info. So I actually do have a local copy of the repository. I want to check against the local repository if that specific test file is the only thing that has changed. – user2554585 Jul 22 '13 at 21:56