How do I check if a file exists in a remote?

后端 未结 2 975
北荒
北荒 2020-12-15 06:39

Is there a way to check if a file under specified, relative path exist in a remote? I\'m fine with fetching the info first if it\'s the only option. In other words I\'m look

2条回答
  •  一生所求
    2020-12-15 07:18

    You can use

    git cat-file -e :
    

    which will exit with zero when the file exists. Instead of above you'd use a remote branch name (but it could in fact be any tree-ish object reference). To use such a remote branch, you'll need to have the remote repository configured and fetched (i.e. by using git remote add + git fetch).

    A concrete example:

    $ git cat-file -e origin/master:README && echo README exists
    README exists
    
    $ git cat-file -e origin/master:FAILME
    fatal: Not a valid object name origin/master:FAILME
    

    Two things to note:

    • Use / as path delimiter in filenames, even on e.g. Windows.
    • is a full path (such as foo/bar/README), relative to the root of the repository.

提交回复
热议问题