git2-rs exposes libgit2 to Rust programs for repository work. Local repository features are enabled by default; optional features add HTTPS and SSH networking.
Overview
git2-rs provides Rust bindings to libgit2, the C library used to manage Git repositories. The crate wraps libgit2 closely while adding stronger types and automatic resource management, including Result-based error handling across interfaces.
By default the library supports local repository operations without network access. Enabling the https and/or ssh Cargo features unlocks cloning and other remote workflows; both are recommended when code accepts user-supplied repository URLs. The bundled libgit2-sys crate vendors libgit2 sources, so a system install is optional—static linking applies when vendoring is on or no suitable system library is found, and dynamic linking is attempted when an appropriate version is present.
Typical use starts from a Repository opened, initialized, or cloned on disk. Most derived objects—commits, branches, diffs, and similar types—tie to that repository’s lifetime so handles cannot outlive the parent repo. The API covers indexing, merging, rebasing, diffs, configuration, remotes, and many other libgit2 capabilities exposed as Rust modules and structs.
git2-rs targets stable Rust and generally supports the prior stable release as well. It requires libgit2 2.0.0-rc.1 or newer patch releases. When the ssh feature is enabled on macOS, builds depend on libssh2 and OpenSSL per upstream crate guidance.
Key features
- Repository open init and clone
- Optional HTTPS remote support
- Optional SSH remote support
- Vendored or dynamic libgit2
- Strongly typed libgit2 APIs
- Automatic resource cleanup
- Merge rebase and diff APIs
- Stable Rust compatibility
Use cases
- Git automation in Rust
- Custom repository tooling
- Embedded Git operations
- CI and build scripts
- Remote clone with SSH or HTTPS