Many workflows require Git operations. Graph-sitter provides a high-level API for common Git operations through the Codebase class, including:

Committing Changes to Git

You can commit changes to Git using the Codebase.git_commit(…):

# Make some changes and call `commit()` to sync them to disk
codebase.functions[0].rename('foo')
codebase.commit()

# Commit all staged changes to git with a message
commit = codebase.git_commit("feat: update function signatures")

# You can also verify the commit (runs pre-commit hooks)
commit = codebase.git_commit("feat: update signatures", verify=True)

# The method returns the commit object if changes were committed, None otherwise
if commit:
    print(f"Created commit: {commit.hexsha}")

git_commit will only commit changes that have been synced to the filesystem by calling Codebase.commit(). See Commit and Reset for more details.

Checking Current Git State

Graph-sitter provides properties to check the current Git state:

# Get the default branch (e.g. 'main' or 'master')
default = codebase.default_branch
print(f"Default branch: {default}")

# Get the current commit
current = codebase.current_commit
if current:
    print(f"Current commit: {current.hexsha}")

Checking Out Branches and Commits

The Codebase.checkout(…) method allows you to switch between branches and commits.

This will automatically re-parse the codebase to reflect the new state.

# Checkout a branch
result = codebase.checkout(branch="feature/new-api")

# Create a new branch if it doesn't exist
result = codebase.checkout(branch="feature/new-api", create_if_missing=True)

# Checkout a specific commit
result = codebase.checkout(commit="abc123")

# Checkout and pull from remote
result = codebase.checkout(branch="main", remote=True)