Check if specific file in git repository has changed

前端 未结 4 827
面向向阳花
面向向阳花 2021-02-05 03:59

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?

相关标签:
4条回答
  • 2021-02-05 04:33

    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
    
    0 讨论(0)
  • 2021-02-05 04:38

    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.

    0 讨论(0)
  • 2021-02-05 04:41

    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.

    0 讨论(0)
  • 2021-02-05 04:53

    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.
    0 讨论(0)
提交回复
热议问题