Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 384
user2554585 Avatar asked Jul 22 '13 21:07

user2554585


People also ask

How does git check if a file has been modified?

Indexing. For every tracked file, Git records information such as its size, creation time and last modification time in a file known as the index. To determine whether a file has changed, Git compares its current stats with those cached in the index. If they match, then Git can skip reading the file again.

How do you check who last modified a file in git?

Git doesn't record the last modification date, only the commit/author dates for a all commit (which can include more than one file). You would need to run a script in order to amend a commit with the last modification date of a particular file (not very useful if said commit has more than one file in it).

Which command is used to view the history of all the changes to a file?

Using git log --follow -p bar will show the file's entire history, including any changes to the file when it was known as foo .


1 Answers

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.

like image 112
Rachel K. Westmacott Avatar answered Sep 20 '22 23:09

Rachel K. Westmacott