Git operations in Snowflake¶
This topic describes how to perform common repository operations.
Integrate a Git repository with your Snowflake account¶
For information about setting up an integration with a Git repository, see Setting up Snowflake to use Git.
Refresh a repository stage from the repository¶
You can refresh the repository stage with a full clone that fetches all branches, tags, and commits from the remote repository. When you do so, you also prune branches and commits that were fetched earlier but no longer exist in the remote repository.
To perform the operations described in this section, you’ll need the Snowflake access described in Access control for ALTER GIT REPOSITORY.
You can refresh your repository stage using either Snowsight or SQL.
When you want to refresh your repository stage, you can fetch contents of the integrated Git repository to the repository stage by using the ALTER GIT REPOSITORY command.
Code in the following example updates the repository stage with the contents of the repository:
ALTER GIT REPOSITORY snowflake_extensions FETCH;
You can use Snowsight to refresh the repository stage (which contains a clone of the remote repository) from the remote repository.
Sign in to Snowsight.
In the navigation menu, select Data » Databases.
In the object explorer, select the database and schema that contain the Git repository stage you want to refresh.
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 refresh the repository stage.
View a list of repository files¶
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 (you can abbreviate LIST to LS):
List by branch name:
LS @repository_stage_name/branches/branch_name;
List by tag name:
LS @repository_stage_name/tags/tag_name;
List by commit hash:
LS @repository_stage_name/commits/commit_hash;
The following example generates output that lists files in the main branch of the Git repository snowflake_extensions
:
LS @snowflake_extensions/branches/main;
The preceding command generates output similar to the following:
-------------------------------------------------------------------------------------------------------------------------------------------------------
| name | size | md5 | sha1 | last_modified |
-------------------------------------------------------------------------------------------------------------------------------------------------------
| snowflake_extensions/branches/main/.gitignore | 10 | | e43b0f988953ae3a84b00331d0ccf5f7d51cb3cf | Wed, 5 Jul 2023 22:42:34 GMT |
-------------------------------------------------------------------------------------------------------------------------------------------------------
| snowflake_extensions/branches/main/python-handlers/filter.py | 169 | | c717137b18d7b75005849d76d89037fafc7b5223 | Wed, 5 Jul 2023 22:42:34 GMT |
-------------------------------------------------------------------------------------------------------------------------------------------------------
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 Data » Databases.
In the object explorer, select the database and schema that contain the Git repository stage you want to view.
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.
Select the branch or tag whose files you want to list.
Below the repository name, view the list of folders and files corresponding to the selection you made.
View repository stage properties¶
You can view the properties associated with a repository stage.
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 repository stage properties by using either Snowsight or SQL.
You can view repository stage 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.
DESCRIBE GIT REPOSITORY snowflake_extensions;
The preceding command generates output similar to the following:
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| CREATED_ON | NAME | DATABASE_NAME | SCHEMA_NAME | ORIGIN | API_INTEGRATION | GIT_CREDENTIALS | OWNER | OWNER_ROLE_TYPE | COMMENT |
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 2023-06-28 08:46:10.886 -0700 | SNOWFLAKE_EXTENSIONS | MY_DB | MAIN | https://github.com/my-account/snowflake-extensions.git | GIT_API_INTEGRATION | MY_DB.MAIN.GIT_SECRET | ACCOUNTADMIN | ROLE | |
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
You can use Snowsight to view repository properties.
Sign in to Snowsight.
In the navigation menu, select Data » Databases.
In the object explorer, select the database and schema that contain the Git repository stage you want to view.
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:
The repository’s origin
The API integration and credentials (specified as a secret) used by Snowflake to interact with the remote repository
Privileges granted on the repository stage
Execute code from a repository¶
You can execute the code contained by a file from the repository.
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 repository stage.
Code in the following example executes code in create-database.sql
from the repository stage snowflake_extensions
:
EXECUTE IMMEDIATE FROM @snowflake_extensions/branches/main/sql/create-database.sql;
You can use Snowsight to execute SQL code from a Git repository.
Note that when you execute code this way, you won’t see output generated by the code’s execution.
Sign in to Snowsight.
In the navigation menu, select Data » Databases.
In the object explorer, select the database and schema that contain the Git repository stage you want to view.
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.
Select the branch or tag containing the file whose code you want to execute.
Beneath the repository name, select the folder containing the file you want to execute.
Locate the file whose code you want to execute, and select » Execute immediate.
In the box that appears, review the code contained by the file.
This is the code that Snowflake will execute.
To execute the displayed code, select Execute Immediate.
The details page displays a notification that the code’s execution was started. It later indicates whether the execution succeeded or failed.
Copy repository-based code into a worksheet¶
You can quickly copy code from a repository file into a worksheet. You can edit and run the copied code or use it as a read-only template for other users.
You can copy the content of the following types of files: .sql
and .py
.
To save your changes in your repository, you need to copy the edited code from the worksheet into a file (such as the file corresponding to the one you copied from) in your local Git repository and commit the changes from there.
- 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 Data » Databases.
In the object explorer, select the database and schema that contain the Git repository stage you want to view.
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.
Select the branch or tag containing the file whose code you want to copy.
Beneath the repository name, select the folder containing the file you want to execute.
Locate the file whose code you want to execute, and select » Copy into worksheet.
Snowflake copies code from the file you selected into a new worksheet.