Reputation: 3669
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
Upvotes: 57
Views: 54865
Reputation: 821
"check if that specific file has changed."
The git diff answers so far will only indicate if the file has changed but not if they have been staged. For example:
To detect if the file is changed locally or if a change has been staged use:
git status -s test
MM test
-- or --
git status -s test
M test
-- or --
git status -s test
M test
PS. git status does not have an --exit-code CLI arg. With a script you have to check for output or no output.
Upvotes: 1
Reputation: 1850
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.
Upvotes: 3
Reputation: 2291
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.
Upvotes: 91
Reputation: 70135
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
Upvotes: 20
Reputation: 9197
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:
Upvotes: 1