A highly extensible git implementation library written in pure Go
npx @tessl/cli install tessl/golang-go-git@5.16.0go-git is a highly extensible Git implementation library written in pure Go. It provides both high-level porcelain operations and low-level plumbing operations for working with Git repositories programmatically.
Module: github.com/go-git/go-git/v5
Version: 5.16.4
License: Apache-2.0
Language: Go
go-git enables you to:
The library is organized into several major layers:
The root package github.com/go-git/go-git/v5 provides high-level operations similar to Git command-line tools:
The plumbing packages provide low-level Git internals:
plumbing/object)plumbing/format)plumbing/transport)storage/filesystem - Standard .git directorystorage/memory - Ephemeral in-memory storagestorage/transactional - Transaction supportThe config package handles Git configuration:
The 47 public packages are organized into these major groups:
| Package Group | Description | Key Packages |
|---|---|---|
| Porcelain | High-level Git operations | git (root) |
| Plumbing Core | Low-level Git primitives | plumbing, plumbing/storer |
| Object Model | Git objects and iteration | plumbing/object, plumbing/filemode |
| Storage | Storage implementations | storage, storage/filesystem, storage/memory |
| Configuration | Config management | config |
| Transport | Network protocols | plumbing/transport, plumbing/transport/http, plumbing/transport/ssh |
| Protocol | Git wire protocol | plumbing/protocol/packp, plumbing/protocol/packp/capability |
| Format | Git file formats | plumbing/format/packfile, plumbing/format/index, plumbing/format/config |
| Utilities | Helper packages | utils/diff, utils/merkletrie, utils/binary |
| Cache | Object caching | plumbing/cache |
For most use cases, you'll need these imports:
import (
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-git/go-git/v5/config"
)For storage backends:
import (
"github.com/go-git/go-git/v5/storage/memory"
"github.com/go-git/go-git/v5/storage/filesystem"
"github.com/go-git/go-billy/v5/memfs"
"github.com/go-git/go-billy/v5/osfs"
)For authentication:
import (
"github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/go-git/go-git/v5/plumbing/transport/ssh"
)package main
import (
"fmt"
"github.com/go-git/go-git/v5"
)
func main() {
// Clone to a local directory
r, err := git.PlainClone("/tmp/repo", false, &git.CloneOptions{
URL: "https://github.com/go-git/go-git",
Progress: os.Stdout,
})
if err != nil {
panic(err)
}
// Get HEAD reference
ref, err := r.Head()
if err != nil {
panic(err)
}
// Get the commit object
commit, err := r.CommitObject(ref.Hash())
if err != nil {
panic(err)
}
fmt.Println(commit)
}package main
import (
"fmt"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/object"
)
func main() {
// Open repository
r, err := git.PlainOpen("/path/to/repo")
if err != nil {
panic(err)
}
// Get HEAD and commit history
ref, err := r.Head()
if err != nil {
panic(err)
}
// Iterate commit history
cIter, err := r.Log(&git.LogOptions{From: ref.Hash()})
if err != nil {
panic(err)
}
err = cIter.ForEach(func(c *object.Commit) error {
fmt.Println(c.Message)
return nil
})
if err != nil {
panic(err)
}
}package main
import (
"time"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/object"
)
func main() {
// Open repository
r, err := git.PlainOpen("/path/to/repo")
if err != nil {
panic(err)
}
// Get worktree
w, err := r.Worktree()
if err != nil {
panic(err)
}
// Add file to staging area
_, err = w.Add("README.md")
if err != nil {
panic(err)
}
// Create commit
commit, err := w.Commit("Update README", &git.CommitOptions{
Author: &object.Signature{
Name: "John Doe",
Email: "john@doe.org",
When: time.Now(),
},
})
if err != nil {
panic(err)
}
obj, err := r.CommitObject(commit)
if err != nil {
panic(err)
}
fmt.Println(obj)
}package main
import (
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/transport/http"
)
func main() {
// Clone with basic auth
r, err := git.PlainClone("/tmp/repo", false, &git.CloneOptions{
URL: "https://github.com/private/repo",
Auth: &http.BasicAuth{
Username: "username",
Password: "password_or_token",
},
})
if err != nil {
panic(err)
}
_ = r
}The documentation is organized into the following capability areas:
Learn how to create, open, and clone repositories. Covers:
Init and PlainInitOpen and PlainOpenClone and PlainCloneWork with the working directory and staging area. Covers:
Interact with remote repositories. Covers:
Access Git objects (commits, trees, blobs, tags). Covers:
Work with Git references (branches, tags, HEAD). Covers:
Understand storage backends and interfaces. Covers:
Manage repository and global configuration. Covers:
Use different transport protocols and authentication. Covers:
Access low-level Git internals. Covers:
type Repository struct {
Storer storage.Storer
// contains filtered or unexported fields
}The main entry point for repository operations. Provides methods for accessing objects, references, remotes, and configuration.
type Worktree struct {
Filesystem billy.Filesystem
Excludes []gitignore.Pattern
// contains filtered or unexported fields
}Represents a working directory. Provides methods for checkout, add, commit, status, and other working directory operations.
type Remote struct {
// contains filtered or unexported fields
}
func (r *Remote) Config() *config.RemoteConfig
func (r *Remote) Fetch(o *FetchOptions) error
func (r *Remote) Push(o *PushOptions) errorRepresents a remote repository with fetch and push capabilities.
type Hash [20]byte
func NewHash(s string) Hash
func (h Hash) String() string
func (h Hash) IsZero() boolRepresents a Git object hash (SHA-1 or SHA-256).
type Reference struct {
// contains filtered or unexported fields
}
func NewHashReference(name ReferenceName, h Hash) *Reference
func NewSymbolicReference(name, target ReferenceName) *Reference
func (r *Reference) Name() ReferenceName
func (r *Reference) Hash() Hash
func (r *Reference) Type() ReferenceTypeRepresents a Git reference (branch, tag, or symbolic reference like HEAD).
go-git uses standard Go error handling. Common errors include:
var (
ErrRepositoryNotExists = errors.New("repository does not exist")
ErrRepositoryAlreadyExists = errors.New("repository already exists")
ErrRemoteNotFound = errors.New("remote not found")
ErrReferenceNotFound = errors.New("reference not found")
ErrObjectNotFound = errors.New("object not found")
ErrInvalidReference = errors.New("invalid reference")
ErrForceNeeded = errors.New("some refs were not updated")
NoErrAlreadyUpToDate = errors.New("already up-to-date")
)Special note: NoErrAlreadyUpToDate is returned when there's nothing to fetch/pull but is not considered an error condition.
Close() method to free resourcesLargeObjectThreshold for repositories with large filesRepository instances for concurrent operationsCheckoutOptions.SparseCheckoutDirectories for partial checkoutsCloneOptions.Depth for shallow clonesLogOptions.PathFilter to filter commit historycache.NewObjectLRU()errors.Is() to check for specific errorsNoErrAlreadyUpToDate appropriately in fetch/pull operationsgo-git v5 requires:
The library implements Git protocol version 2 and supports: