user2554585
user2554585

Reputation: 3669

Check if specific file in git repository has changed

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

Answers (5)

JohnA
JohnA

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:

  • modify file "test"
  • run "git diff --exit-code test" and that will work just fine
  • run "git add test" now test is staged
  • but now "git diff --exit-code test" will not detect it has changed

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
  • A " M" means local changes only.
  • A "M " means staged changes only.
  • Two "MM" means local changes and staged changes as well.
  • an empty response means no local changes and no staged changes

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

GoodDok
GoodDok

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

Rachel K. Westmacott
Rachel K. Westmacott

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

GoZoner
GoZoner

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

Peter Lundgren
Peter Lundgren

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:

  • 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.

Upvotes: 1

Related Questions