git 如何检查远程文件中是否存在文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4135049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 04:44:08  来源:igfitidea点击:

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

gitgit-remote

提问by zaza

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-ls-files。我只对文件是否存在感兴趣(分支上的文件列表也可以),我不关心哈希、差异等。

回答by earl

You can use

您可以使用

git cat-file -e <remote>:<filename>

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).

当文件存在时,它将以零退出。<remote>您可以使用远程分支名称而不是上面的名称(但实际上它可以是任何树状对象引用)。要使用这样的远程分支,您需要配置和获取远程存储库(即使用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.
  • <filename>is a full path (such as foo/bar/README), relative to the root of the repository.
  • 使用/在文件名路径分隔符,即使是在如Windows。
  • <filename>foo/bar/README相对于存储库根目录的完整路径(例如)。

回答by Chris Johnsen

You can use git archiveto access individual files without downloading any other part of a repository:

您可以使用git archive来访问单个文件,而无需下载存储库的任何其他部分:

if git archive --format=tar \
               --remote=<remote_name-or-URL> master README >/dev/null; then
  echo 'master has README'
else
  echo 'master does not have README (or other error)'
fi

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.

存档服务 ( upload-archive) 可能不会在所有服务器或存储库上启用,但您必须针对需要访问的服务器和存储库对其进行测试。

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):

如果您还没有相关存储库的远程设置,您可以对 FETCH_HEAD 执行“浅”获取(这需要在 Git 存储库中完成,但它可以完全不相关甚至是空的):

git fetch --depth=1 remote_name-or-URL master
if git rev-parse --verify --quiet FETCH_HEAD:README >/dev/null; then
  echo "repository's master has README"
else
  echo "repository's master does not have README"
fi

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:

如果您为存储库定义了远程,那么您可能只想更新它并通过正常的远程跟踪分支访问文件:

git fetch remote_name
if git rev-parse --verify --quiet remote_name/master:README >/dev/null; then
  echo "remote's master has README"
else
  echo "remote's master does not have README"
fi