如何检查远程文件是否存在?
有没有办法检查指定相对路径下的文件是否存在于远程中?如果这是唯一的选择,我可以先获取信息。换句话说,我正在寻找带有指定远程和分支选项的 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
当文件存在时, 它将以零退出。您可以使用远程分支名称,而不是上面的
(但它实际上可以是任何树形对象引用)。要使用这样的远程分支,您需要配置并获取远程存储库(即通过使用git remote add
+git fetch
)。一个具体的例子:
需要注意的两件事:
/
作为文件名中的路径分隔符,即使在Windows等系统上也是如此。
是相对于存储库根目录的完整路径(例如foo/bar/README
)。You can use
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 usinggit remote add
+git fetch
).A concrete example:
Two things to note:
/
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.您可以使用 git archive 访问单个文件,而无需下载存储库的任何其他部分:
存档服务 (
upload-archive
) 可能不会在所有服务器或存储库上启用,但您必须针对您要使用的服务器和存储库对其进行测试。需要访问。如果存档服务不可用,您将必须通过正常方式获取对象。
如果您还没有对相关存储库进行远程设置,您可以对 FETCH_HEAD 进行“浅层”提取(这需要在 Git 存储库中完成,但它可以完全不相关,甚至是空的):
如果您有为存储库定义的远程,那么您可能只想更新它并通过正常的远程跟踪分支访问该文件:
You can use git archive to access individual files without downloading any other part of a repository:
The archive service (
upload-archive
) may not be enabled on all servers or repositories though, you will have to test it for the servers and repositories that you need to access.If the archive service is not available, you will have to fetch objects through normal means.
If you do not already have a remote setup for the repository in question you can do a “shallow” fetch into FETCH_HEAD (this needs to be done in a Git repository, but it can be completely unrelated or even empty):
If you have a remote defined for the repository, then you probably just want to update it and access the file through the normal remote-tracking branches: