如何检查远程文件是否存在? [英] How do I check if a file exists in a remote?
问题描述
有没有一种方法可以检查远程中是否存在指定的相对路径下的文件?如果唯一的选择,我可以先获取信息.换句话说,我正在寻找带有指定远程和分支的选项的git-ls-files.我只对文件是否存在感兴趣(分支上的文件列表也可以),我不在乎哈希,差异等.
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 looking for git-ls-files with an option to specify remote and branch. I'm only interested if the file exists (a list of files on the branch will do as well), I don't care about hashes, diffs etc.
推荐答案
您可以使用
git cat-file -e <remote>:<filename>
,当文件存在时,它将以零退出.您可以使用远程分支名称代替上面的<remote>
(但实际上它可以是任何树状对象引用).要使用这样的远程分支,您需要配置和获取远程存储库(即使用git remote add
+ git fetch
).
which will exit with zero when the file exists. Instead of <remote>
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
).
一个具体示例:
$ 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
需要注意的两件事:
- 在文件名中使用
/
作为路径分隔符,即使在例如Windows. -
<filename>
是相对于存储库根目录的完整路径(例如foo/bar/README
).
- Use
/
as path delimiter in filenames, even on e.g. Windows. <filename>
is a full path (such asfoo/bar/README
), relative to the root of the repository.
这篇关于如何检查远程文件是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!