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.

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;
Copy

View a list of repository branches or tags

You can view a list of branches and tags available in the repository stage that represents a clone of your Git repository.

To perform the operations described in this section, you’ll need the Snowflake access described in the following topics:

  • For listing branches: Access control for SHOW GIT BRANCHES

  • For listing tags: Access control for SHOW GIT TAGS

    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:

    SHOW GIT BRANCHES IN snowflake_extensions;
    
    Copy

    The preceding command generates output similar to the following:

    --------------------------------------------------------------------------------
    | name | path           | checkouts | commit_hash                              |
    --------------------------------------------------------------------------------
    | main | /branches/main |           | 0f81b1487dfc822df9f73ac6b3096b9ea9e42d69 |
    --------------------------------------------------------------------------------
    

View a list of repository files

You can view a list of files in a branch, tag, or commit.

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;
    
    Copy
  • List by tag name:

    LS @repository_stage_name/tags/tag_name;
    
    Copy
  • List by commit hash:

    LS @repository_stage_name/commits/commit_hash;
    
    Copy

The following example generates output that lists files in the main branch of the Git repository snowflake_extensions:

LS @snowflake_extensions/branches/main;
Copy

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

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.

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;
Copy

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

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.

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;
Copy

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.