The git module is a lightweight wrapper around common git operations. For more complex operations, you can execute commands directly using the bash utility. Credentials are automatically configured by default.

Default configuration:

user.name=BlocksOrg
user.email=bot@blocksorg.com
remote.origin.url=git@github.com:YourOrg/repo.git # The remote url of the repo which triggered the automation

Example Usage

from blocks import git

# Clone a repository
git.clone(target_dir="my-project", ref="develop")

# Make changes and commit
git.add(all=True)
git.commit("Update README.md")

# Push changes to origin
git.push(publish=True)

Methods

add

git.add(file, all=False)

Stages a file or files for commit. If all is True, stages all changes.

file
string

File path to add (ignored if all=True).

all
bool

If True, adds all changes instead of a single file.

branch

git.branch(branch_name, checkout=False)

Creates a new branch by default. If checkout is True, creates and checks out (git checkout -b branch-name), otherwise just creates a branch with branch-name.

branch_name
string

The name of the new branch.

checkout
bool

Whether to create and checkout the branch.

checkout

git.checkout(target_dir="repo", ref="", new_branch=False)

Clones a repository into target_dir. If ref is provided, it checks out that specific branch/tag.

target_dir
string

The local directory for the clone.

ref
string

The Git ref (branch, tag) to clone. Defaults to "" (clone the default branch).

new_branch
bool

If True, this indicates the intention to create a new branch, but is not currently used in the command.

clone

git.clone(target_dir="repo", ref="", new_branch=False)

An alias for checkout, providing the same behavior for consistency.

target_dir
string

The local directory for the clone.

ref
string

The Git ref (branch, tag) to clone. Defaults to "" (clone the default branch).

new_branch
bool

If True, this indicates the intention to create a new branch, but is not currently used in the command.

commit

git.commit(message)

Commits staged changes with a given commit message.

message
string

The commit message.

init

git.init()

Initializes a new Git repository locally.

pull

git.pull()

Pulls from the repository specified by the class’s constructed remote URL. It runs git pull origin=url HEAD.

push

git.push(publish=False)

Pushes the current HEAD to the remote. If publish is True, it pushes with the -u origin HEAD flag, setting the upstream branch.

publish
bool

Whether to set upstream on push.