Working with Git repositories¶
This topic describes how to perform common repository operations using SQL commands and Snowsight.
您还可以将以下功能与 Git 一起使用,每种功能都有自己执行 Git 操作的方式:
从远程 Git 存储库中提取¶
您可以在 Snowflake 中将远程存储库的所有分支、标签和提交提取到 Git 存储库克隆中。执行此操作时,您还会削减那些较早提取但远程存储库中已不再存在的分支和提交。
To perform the operations described in this section, you’ll need the Snowflake access described in Access control for ALTER GIT REPOSITORY.
You can fetch from the remote Git repository using either Snowsight or SQL.
You can fetch from the remote Git repository to the Git repository clone in Snowflake by using the ALTER GIT REPOSITORY command.
以下示例中的代码将使用存储库的内容更新 Git 存储库克隆:
You can use Snowsight to fetch from the remote repository.
- Sign in to Snowsight.
- In the navigation menu, select Catalog » Database Explorer.
- 在对象资源管理器中,选择包含您要提取的 Git 存储库克隆的数据库和架构。
- Inside the schema, open Git Repositories.
- In Git Repositories, select the repository to view the details page.
- In the repository details, on the Files Explorer tab, select the Fetch button to fetch from the remote repository.
查看存储库分支或标签的列表
您可以查看从远程存储库提取的 Snowflake Git 存储库克隆中可用的分支和标签列表。
要执行本部分中介绍的操作,您需要以下主题中介绍的 Snowflake 访问权限:
- For listing branches: Access control for SHOW GIT BRANCHES
- For listing tags: Access control for SHOW GIT TAGS
You can view a list of branches or tags using either Snowsight or SQL.
You can view branches and tags by using the SHOW GIT BRANCHES and SHOW GIT TAGS commands.
The following example generates output that lists branches in the Git repository snowflake_extensions:
如前所述的命令会生成类似下面的输出:
You can use Snowsight to view a list of the files in your Git repository.
- Sign in to Snowsight.
- In the navigation menu, select Catalog » Database Explorer.
- 在对象资源管理器中,选择包含要查看的 Git 存储库克隆的数据库和架构。
- Inside the schema, open Git Repositories.
- 找到并选择存储库以查看其详细信息页面。
- In the repository’s details page, on the Files Explorer tab, select the Branch button.
- From the Branch drop-down menu, do one of the following:
- To view a list of branches cloned from the repository, select Branches .
- To view a list of the tags cloned from the repository, select Tags.
查看存储库文件列表
You can view a list of files in a branch, tag, or commit using either Snowsight or SQL.
You can view a list of files in the repository by using the LIST command in the following forms, specifying the Git repository clone as you would a stage (you can abbreviate LIST to LS):
-
按分支名称列出:
-
按标签名称列出:
-
按提交哈希列出:
The following example generates output that lists files in the main branch of the Git repository snowflake_extensions:
The preceding command generates output similar to the following. For descriptions of the output columns, see the LIST command reference.
You can use Snowsight to view a list of the branches and tags in your Git repository.
-
Sign in to Snowsight.
-
In the navigation menu, select Catalog » Database Explorer.
-
在对象资源管理器中,选择包含要查看的 Git 存储库克隆的数据库和架构。
-
Inside the schema, open Git Repositories.
-
Inside Git Repositories, select a repository to view its details page.
-
In the repository’s details page, on the Files Explorer tab, select the Branch button.
-
From the Branch drop-down menu, select one of the following:
- To view a list of branches cloned from the repository, select Branches.
- To view a list of the tags cloned from the repository, select Tags.
-
选择要列出的文件所属的分支或标签。
-
在存储库名称下方,查看与您选择的内容对应的文件夹和文件的列表。
执行存储库中的代码
可以执行存储库中的文件所包含的代码。
To perform the operations described in this section, you’ll need the Snowflake access described in Access control for EXECUTE IMMEDIATE FROM.
You can execute code by using either Snowsight or SQL.
You can use EXECUTE IMMEDIATE FROM to execute code in a Git repository clone.
Code in the following example executes code in create-database.sql from the Git repository clone snowflake_extensions:
You can use Snowsight to execute SQL code from a Git repository.
请注意,当您以这种方式执行代码时,您将看不到代码执行生成的输出。
-
Sign in to Snowsight.
-
In the navigation menu, select Catalog » Database Explorer.
-
在对象资源管理器中,选择包含要查看的 Git 存储库克隆的数据库和架构。
-
Inside the schema, open Git Repositories.
-
Inside Git Repositories, select a repository to view its details page.
-
In the repository’s details page, on the Files Explorer tab, select the Branch button.
-
From the Branch drop-down menu, select one of the following:
- Branches to view a list of branches cloned from the repository.
- Tags to view a list of the tags cloned from the repository.
-
选择包含要执行的代码所属文件的分支或标签。
-
在存储库名称下,选择包含要执行的文件的文件夹。
-
Locate the file whose code you want to execute, and select
» Execute immediate. -
在显示的框中,查看文件包含的代码。
这是 Snowflake 将要执行的代码。
- To execute the displayed code, select Execute Immediate.
详细信息页面会显示一条通知,指明代码执行开始。它稍后会指示执行是成功还是失败。
将基于存储库的代码复制到工作表中
您可以将代码从存储库文件快速复制到工作表中。您可以编辑和运行复制的代码,或者将其作为其他用户的只读模板。
You can copy the content of the following types of files: .sql and .py.
要将更改保存在存储库中,您需要将工作表中的编辑代码复制到本地 Git 存储库中的文件(例如,与从中复制的文件对应的文件)中,然后从那里提交更改。
- Snowsight:
You can use Snowsight to copy content from a file in your repository into a worksheet.
-
Sign in to Snowsight.
-
In the navigation menu, select Catalog » Database Explorer.
-
在对象资源管理器中,选择包含要查看的 Git 存储库克隆的数据库和架构。
-
Inside the schema, open Git Repositories.
-
Inside Git Repositories, select a repository to view its details page.
-
In the repository’s details page, on the Files Explorer tab, select the Branch button.
-
From the Branch drop-down menu, do one of the following:
- To view a list of branches cloned from the repository, select Branches.
- To view a list of the tags cloned from the repository, select Tags.
-
选择包含您要复制的代码所属文件的分支或标签。
-
在存储库名称下,选择包含要执行的文件的文件夹。
-
Locate the file whose code you want to execute, and select
» Copy into worksheet.
Snowflake 将所选文件中的代码复制到新工作表中。
-
查看 Git 存储库克隆属性¶
您可以在 Snowflake 中查看与 Git 存储库克隆相关的属性。
To perform the operations described in this section, you’ll need the Snowflake access described in Access control for DESC GIT REPOSITORY.
You can view Git repository clone properties by using either Snowsight or SQL.
You can view Git repository clone properties by using the SQL commands SHOW GIT REPOSITORIES and DESCRIBE GIT REPOSITORY.
The properties information includes the Git origin URL, name of the API integration and credentials (specified as a secret) used to connect with the remote repository, and so on.
如前所述的命令会生成类似下面的输出:
You can use Snowsight to view repository properties.
- Sign in to Snowsight.
- In the navigation menu, select Catalog » Database Explorer.
- 在对象资源管理器中,选择包含要查看的 Git 存储库克隆的数据库和架构。
- Inside the schema, open Git Repositories.
- Inside Git Repositories, select a repository to view its details page.
- In the repository’s details page, select the Git Repository Details tab to view information that includes the following details:
- 存储库的来源
- The API integration and credentials (specified as a secret) used by Snowflake to interact with the remote repository
- 对 Git 存储库克隆授予的权限