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?
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
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.
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.
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: