# Make some changes and call `commit()` to sync them to diskcodebase.functions[0].rename('foo')codebase.commit()# Commit all staged changes to git with a messagecommit = 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 otherwiseif 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.
Graph-sitter provides properties to check the current Git state:
Copy
Ask AI
# Get the default branch (e.g. 'main' or 'master')default = codebase.default_branchprint(f"Default branch: {default}")# Get the current commitcurrent = codebase.current_commitif current: print(f"Current commit: {current.hexsha}")
The Codebase.checkout(…) method allows you to switch between branches and commits.This will automatically re-parse the codebase to reflect the new state.
Copy
Ask AI
# Checkout a branchresult = codebase.checkout(branch="feature/new-api")# Create a new branch if it doesn't existresult = codebase.checkout(branch="feature/new-api", create_if_missing=True)# Checkout a specific commitresult = codebase.checkout(commit="abc123")# Checkout and pull from remoteresult = codebase.checkout(branch="main", remote=True)